Pri*_*ico 5 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)))
});
}
Run Code Online (Sandbox Code Playgroud)
这适合我.我希望它能帮助那些希望将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);
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
953 次 |
最近记录: |