在 ASP.NET Core 2.0 中从另一个(无用户交互)授权一个应用程序服务
Authorize one app service from another (no user interaction) in ASP.NET Core 2.0
我在面向 Azure 的互联网 (PubWeb) 中有一个 Web API 应用程序,因此可以在任何地方使用它。我创建了一个 Web API(同样,在 Azure 中,我们称它为 InWeb)。我想要强大的安全规则:
- InWeb 只能由 PubWeb 使用。这可能涉及一些网络安全 - 这是一个简单的部分(在 Azure 中)。
- PubWeb 必须以某种方式由 Azure AD 授权才能使用 InWeb 服务。想到服务主体、证书等。但我不确定。
那么 2 的最佳解决方案是什么?
更新 - 这是 InWeb Core 2.0 Auth 设置:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddAzureAdBearer(options => Configuration.Bind("AzureAd", options));
services.AddMvc(options => { options.InputFormatters.Add(new TextPlainInputFormatter()); });
}
您必须将两个 API 都注册为 Azure AD 中的应用程序。这可以通过 Azure Active Directory blade 的 App registrations.
完成
现在我假设对 PubWeb 的调用未使用 Azure AD 不记名令牌进行身份验证。这意味着您唯一的选择是在调用 InWeb 时使用应用程序权限和客户端凭据授予。
要定义应用程序权限,您必须在 InWeb 的清单中添加 "app role"。您可以通过单击 Azure AD 中 InWeb 应用程序 blade 中的“清单”按钮来打开清单。
这是一个示例应用程序角色:
"appRoles": [
{
"allowedMemberTypes": [
"Application"
],
"displayName": "Read all things",
"id": "32028ccd-3212-4f39-3212-beabd6787d81",
"isEnabled": true,
"description": "Allow the application to read all things as itself.",
"value": "Things.Read.All"
}
],
您应该为其生成一个新的 ID,并将显示名称和描述设置为您想要的任何内容。 值 是 PubWeb 调用 InWeb 时将出现在令牌中的内容。
现在您必须转到 PubWeb 的 blade,然后转到所需权限。
在那里你必须去添加 -> Select InWeb -> 勾选你刚刚定义的应用程序权限。
然后单击授予权限。现在 PubWeb 有权调用 InWeb.
您还需要为 PubWeb 生成一个 key/client 秘密。这基本上就是它的密码。您还需要 PubWeb 的客户端 id/application id。
您还需要 InWeb 的 App ID URI。您可以从 Azure AD 中 blade 的属性中找到它。这将是我们将从 PubWeb 获取令牌的资源。
您还需要 Azure AD 的 ID。您可以从主 Azure AD blade.
的属性中找到它
现在要获取令牌,您需要使用 OAuth2 客户端凭据授予流程。最简单的方法是使用像 ADAL 这样的库:https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-authentication-libraries.
如果您想手动执行此操作,可以在此处找到有关 HTTP 调用的详细信息:https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-service-to-service#service-to-service-access-token-request
看起来像这样:
POST /contoso.com/oauth2/token HTTP/1.1
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&client_id=625bc9f6-3bf6-4b6d-94ba-e97cf07a22de&client_secret=qkDwDJlDfig2IpeuUZYKH1Wb8q1V0ju6sILxQQqhJ+s=&resource=https%3A%2F%2Fservice.contoso.com%2F
设置client id为PubWeb的client id,client secret为PubWeb的client secret。资源应该是InWeb的App ID URI。
您应该收到一个访问令牌作为响应,您需要将其作为 header:
附加到您向 InWeb 发出的请求
Authorization: Bearer access-token-here
现在当然 InWeb 也必须验证令牌。这是一个 .NET 示例应用程序(也有其他示例):https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-oauth2-appidentity/blob/master/TodoListService/App_Start/Startup.Auth.cs
该项目实际上还有一个客户端调用 API,客户端凭据流:https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-oauth2-appidentity/blob/master/TodoListWebApp/Controllers/TodoListController.cs#L95。
抱歉回答冗长,我想说得更详细一点:)
但如果您对某些特定部分有疑问,请随时提出。
我在面向 Azure 的互联网 (PubWeb) 中有一个 Web API 应用程序,因此可以在任何地方使用它。我创建了一个 Web API(同样,在 Azure 中,我们称它为 InWeb)。我想要强大的安全规则:
- InWeb 只能由 PubWeb 使用。这可能涉及一些网络安全 - 这是一个简单的部分(在 Azure 中)。
- PubWeb 必须以某种方式由 Azure AD 授权才能使用 InWeb 服务。想到服务主体、证书等。但我不确定。
那么 2 的最佳解决方案是什么?
更新 - 这是 InWeb Core 2.0 Auth 设置:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddAzureAdBearer(options => Configuration.Bind("AzureAd", options));
services.AddMvc(options => { options.InputFormatters.Add(new TextPlainInputFormatter()); });
}
您必须将两个 API 都注册为 Azure AD 中的应用程序。这可以通过 Azure Active Directory blade 的 App registrations.
完成现在我假设对 PubWeb 的调用未使用 Azure AD 不记名令牌进行身份验证。这意味着您唯一的选择是在调用 InWeb 时使用应用程序权限和客户端凭据授予。
要定义应用程序权限,您必须在 InWeb 的清单中添加 "app role"。您可以通过单击 Azure AD 中 InWeb 应用程序 blade 中的“清单”按钮来打开清单。
这是一个示例应用程序角色:
"appRoles": [
{
"allowedMemberTypes": [
"Application"
],
"displayName": "Read all things",
"id": "32028ccd-3212-4f39-3212-beabd6787d81",
"isEnabled": true,
"description": "Allow the application to read all things as itself.",
"value": "Things.Read.All"
}
],
您应该为其生成一个新的 ID,并将显示名称和描述设置为您想要的任何内容。 值 是 PubWeb 调用 InWeb 时将出现在令牌中的内容。
现在您必须转到 PubWeb 的 blade,然后转到所需权限。
在那里你必须去添加 -> Select InWeb -> 勾选你刚刚定义的应用程序权限。
然后单击授予权限。现在 PubWeb 有权调用 InWeb.
您还需要为 PubWeb 生成一个 key/client 秘密。这基本上就是它的密码。您还需要 PubWeb 的客户端 id/application id。
您还需要 InWeb 的 App ID URI。您可以从 Azure AD 中 blade 的属性中找到它。这将是我们将从 PubWeb 获取令牌的资源。
您还需要 Azure AD 的 ID。您可以从主 Azure AD blade.
的属性中找到它现在要获取令牌,您需要使用 OAuth2 客户端凭据授予流程。最简单的方法是使用像 ADAL 这样的库:https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-authentication-libraries.
如果您想手动执行此操作,可以在此处找到有关 HTTP 调用的详细信息:https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-service-to-service#service-to-service-access-token-request
看起来像这样:
POST /contoso.com/oauth2/token HTTP/1.1
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&client_id=625bc9f6-3bf6-4b6d-94ba-e97cf07a22de&client_secret=qkDwDJlDfig2IpeuUZYKH1Wb8q1V0ju6sILxQQqhJ+s=&resource=https%3A%2F%2Fservice.contoso.com%2F
设置client id为PubWeb的client id,client secret为PubWeb的client secret。资源应该是InWeb的App ID URI。
您应该收到一个访问令牌作为响应,您需要将其作为 header:
附加到您向 InWeb 发出的请求Authorization: Bearer access-token-here
现在当然 InWeb 也必须验证令牌。这是一个 .NET 示例应用程序(也有其他示例):https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-oauth2-appidentity/blob/master/TodoListService/App_Start/Startup.Auth.cs
该项目实际上还有一个客户端调用 API,客户端凭据流:https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-oauth2-appidentity/blob/master/TodoListWebApp/Controllers/TodoListController.cs#L95。
抱歉回答冗长,我想说得更详细一点:)
但如果您对某些特定部分有疑问,请随时提出。