带有 Web API 的 Azure B2C

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

我看到的使用 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 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);
            }
        }
    });
}

关于带有 Web API 的 Azure B2C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37872999/

相关文章:

linux - 如何使用Cloud Shell在Linux虚拟机上扩展操作系统磁盘?

asp.net - Web API 2 身份./Token 总是返回404错误

azure - 自定义 Azure AD B2C 登录页面

azure - WebJob 从 Azure DevOps Pipeline 部署,但未显示在 WebApp 下

azure - 如何将 Azure 应用服务域用于静态 Web 应用?

azure - 存在 DNS 详细信息时重用资源管理器模板

使用 Azure AD 作为身份提供者的 Azure ADB2C 单点登录

asp.net-web-api - 使用 WebAPI 中的 Jwt Bearer Authentication 中间件自定义 WWW-Authenticate 质询 header

c# - Microsoft Owin 日志记录 - Web Api 2 - 如何创建记录器?

azure-ad-b2c - 是否可以使用声明转换从 stringCollection 中检索指定索引中的单个项目?