【问题标题】:Azure B2C with Web API带有 Web API 的 Azure B2C
【发布时间】:2016-06-17 04:12:01
【问题描述】:

我看到的将 Azure B2C 与 Web API 结合使用的示例显示 app.UseOAuthBearerAuthentication(如下所示),但是我的 ASP .NET 5 Web API 项目使用 IApplicationBuilder(不是 IAppBuilder)并且 UseOAuthBearerAuthentication 不存在。我已经尝试过 app.UseOpenIdConnectAuthentication,但是我相信这使用了 cookie,并且我无法使用 Xamarin 应用程序作为客户端来使其工作。我已经尝试过 app.UseWindowsAzureActiveDirectoryBearerAuthentication 但我相信这是针对标准 Azure AD(不是 B2C)的,这是真的吗?任何想法如何让 Azure B2C 与最新的 ASP .NET Web API 一起工作?

谢谢!!!

    public void ConfigureAuth(IAppBuilder app)
    {   
        TokenValidationParameters tvps = new TokenValidationParameters
        {
            // This is where you specify that your API only accepts tokens from its own clients
            ValidAudience = clientId,
        };

        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
        {   
            // This SecurityTokenProvider fetches the Azure AD B2C metadata & signing keys from the OpenIDConnect metadata endpoint
            AccessTokenFormat = new JwtFormat(tvps, new OpenIdConnectCachingSecurityTokenProvider(String.Format(aadInstance, tenant, "v2.0", discoverySuffix, commonPolicy)))
        });
    }

【问题讨论】:

    标签: azure asp.net-web-api2 azure-ad-b2c


    【解决方案1】:

    这对我有用。我希望它可以帮助那些希望将 Azure B2C 与最新的 .NET Web API 框架一起使用的人:

    public void ConfigureAuth(IApplicationBuilder app, IOptions<PolicySettings> policySettings)
    {
        app.UseJwtBearerAuthentication(new JwtBearerOptions
        {
            AuthenticationScheme = JwtBearerDefaults.AuthenticationScheme,
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            MetadataAddress = "https://login.microsoftonline.com/[my-tenant].onmicrosoft.com/v2.0/.well-known/openid-configuration?p=B2C_1_my-signup-signin-policy",
            Audience = "[My-Azure-App-Guid]",
            Events = new JwtBearerEvents
            {
                OnTokenValidated= ctx =>
                {
                    var nameClaim = ctx.AuthenticationTicket.Principal.FindFirst("name");
                    if (nameClaim != null)
                    {
                        var claimsIdentity = (System.Security.Claims.ClaimsIdentity)ctx.AuthenticationTicket.Principal.Identity;
                        claimsIdentity.AddClaim(new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Name, nameClaim.Value));
                    }
                    return Task.FromResult(0);
                },
                OnAuthenticationFailed = ctx =>
                {
                    ctx.SkipToNextMiddleware();
                    return Task.FromResult(0);
                }
            }
        });
    }
    

    【讨论】:

    • 这个答案似乎使用.NetCore。任何使用标准 .Net Jwt 库的解决方案?
    【解决方案2】:

    如果 JWT 令牌在您的客户端中可用,那么您可以在 Web API 中使用 JwtBearerAuthentication。 Azure B2C(社交登录)发布的 JWT 可用作令牌以在 Web API 中进行身份验证。

    参考https://github.com/sendhilkumarg/AzureB2CWebAppAndAPIAuthentication 在此示例中,客户端是一个 Web 应用程序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-08
      • 1970-01-01
      • 2017-01-04
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多