azure - 在 Azure Active Directory (AAD) 中注册微服务以确保安全

标签 azure asp.net-web-api owin azure-active-directory microservices

我在 Service Fabric 集群中部署了一个 Service Fabric 应用程序(无状态和有状态)。我正在尝试在应用程序中实现安全性。应用程序使用 Active Directory 身份验证库 (ADAL) 通过 OAuth 2.0 客户端凭据流从 Azure AD 获取 token ,其中客户端凭据是密码。我可以通过在 Azure 门户中注册普通 Web API 应用程序来实现相同的场景。谁能告诉我如何使用 Owin 公开的 WebApi 注册 Service Fabric 微服务应用程序。我在注册回复 URL 和登录 URL 时遇到困难,因为 URL 是动态的(对于有状态分区 ID 和副本 ID)。我在调用相应服务时收到未经授权的访问。我不确定在 azure Active Directory 中添加应用程序时必须为有状态或无状态应用程序注册什么 url。您能否建议我哪里错了以及如何实现。

最佳答案

Can anyone tell me how to register a service fabric microservice application with WebApi exposed using Owin. i have difficulties registering the reply url and sign on url as the urls are dynamic(for statefull partitionid and replica id).

客户端凭据流用于服务或守护程序应用程序。当我们使用客户端凭证流获取 token 时,不需要使用redirect_url。您可以注册任何验证redirect_url。以下是使用客户端凭据的示例:

POST https://login.microsoftonline.com/<tenantId>/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials
&client_id=<clientId>
&client_secret=<clientSecret>
&resource=<app id uri of your web api >

这与使用 Azure 服务结构通过 Web API 与 Azure AD 集成相同。以下是一个示例供您引用:

1.在Azure门户上注册用于保护Web API的Web应用程序(app1)

2.注册一个Web应用程序(app2)作为客户端来请求Web API

3.从门户将 app1 授予 app2

4.使用无状态 Web API 模板创建 Service Fabric 应用程序

5.配置 Service Fabric 应用程序的 app.config

<add key="ida:Audience" value="app id Uri of app1" />
<add key="ida:Tenant" value="tenantId" />

6.安装包 Microsoft.Owin.Security.ActiveDirectory

Install-Package Microsoft.Owin.Security.ActiveDirectory

7.修改启动代码如下:( 注意:方法 appBuilder.UseWindowsAzureActiveDirectoryBearerAuthentication 在之前 appBuilder.UseWebApi(config).

public static void ConfigureApp(IAppBuilder appBuilder)
        {
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            appBuilder.UseWindowsAzureActiveDirectoryBearerAuthentication(
               new WindowsAzureActiveDirectoryBearerAuthenticationOptions
               {
                   Audience = ConfigurationManager.AppSettings["ida:Audience"],
                   Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
                   TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
                   {
                       ValidateIssuer = false
                   }
               });

            appBuilder.UseWebApi(config);
        }
  • 运行 Service Fabric 应用程序
  • 使用上述客户端凭证流程获取 token (clientId和clientSecret来自app2)
  • 通过 Service Fabric 应用程序使用访问 token 请求公共(public)服务,并且运行良好
  • 关于azure - 在 Azure Active Directory (AAD) 中注册微服务以确保安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40890804/

    相关文章:

    c# - 如果用户键入特定字符串,Azure 机器人将跳过对话

    c# - 如何在表单数据/[FromForm] 中发送可为空的 DateTime

    c# - OWIN 启动不工作

    asp.net-mvc-5 - MVC 5 ASP.NET 标识 2 : Capture user's preference for "remember me" in ExternalLogin

    azure - 在 Azure 移动服务的响应中包含创建的项目 ID

    azure - Azure WebJobs 中的 BlobTrigger 不会在 WebJob 启动后添加的文件上触发

    azure - 可靠的有状态服务与事件中心

    asp.net - HTTP模块和OWIN中间件的区别

    c# - 处理服务结构中的异常

    asp.net-web-api - 不带 IQueryable 的 OData