Asp.Net Core 2.0 和 Azure AD B2C,用于在 WebApp 和 API 上进行身份验证
Asp.Net Core 2.0 and Azure AD B2C for authentication on WebApp and API
我有一个用于测试的现有小应用程序,它在 Web 应用程序和 API 的 Asp.Net Core 1.1 中,身份验证是使用 Azure AD B2C 完成的。
我正在尝试将它移动到 .Net Core 2.0,但我不知道如何让它工作,我尝试使用 GitHub Azure-Samples for Web App 和 API 中的两个示例,但我有尝试访问 api 时出现未经授权或 500 错误,如果您有一个使用 2.0 并受 AD B2C 保护的 Web 应用程序调用 Web api 的工作示例,我们将不胜感激。
编辑:
我用来测试的样本是:
网络应用程序:WebApp-OpenIDConnect-DotNet core2.0
网站 Api:B2C-WebApi core2.0
,我更改了 appsettings 值以匹配我的 b2c 目录。
对于我的 asp.net 核心 1.1 测试应用程序,我使用与上面相同的示例,但来自 master 分支,appsettings 具有相同的值。
编辑 2
默认情况下,在 startup.cs 我有这个:
services.AddAuthentication()
.AddJwtBearer(option => new JwtBearerOptions
{
Authority = string.Format("https://login.microsoftonline.com/tfp/{0}/{1}/v2.0/",
Configuration["Authentication:AzureAd:Tenant"], Configuration["Authentication:AzureAd:Policy"]),
Audience = Configuration["Authentication:AzureAd:ClientId"],
Events = new JwtBearerEvents
{
OnAuthenticationFailed = AuthenticationFailed
}
});
这给了我以下错误:
Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求开始 HTTP/1.1 GET http://localhost:44352/api/values/5
Microsoft.AspNetCore.Server.Kestrel:错误:连接 ID“0HL89JHF4VBLM”,请求 ID“0HL89JHF4VBLM:00000001”:应用程序引发了未处理的异常。
System.InvalidOperationException: 未指定 authenticationScheme,也未找到 DefaultChallengeScheme。
如果这样修改services.AddAuthentication
services.AddAuthentication(sharedOption =>
{
sharedOption.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
现在的错误是
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:信息:验证令牌 xxx 失败。
Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException:IDX10500:签名验证失败。没有提供安全密钥来验证签名。
在 System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(字符串令牌,TokenValidationParameters validationParameters)
在 System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(字符串令牌、TokenValidationParameters validationParameters、SecurityToken& validatedToken)
在 Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.d__6.MoveNext()
我在样本上看到了一个拉取请求,它纠正了这个问题 (Link),services.AddAuthentication 必须更改为:
services.AddAuthentication(options =>
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(jwtOptions =>
{
jwtOptions.Authority = $"https://login.microsoftonline.com/tfp/{Configuration["Authentication:AzureAd:Tenant"]}/{Configuration["Authentication:AzureAd:Policy"]}/v2.0/";
jwtOptions.Audience = Configuration["Authentication:AzureAd:ClientId"];
jwtOptions.Events = new JwtBearerEvents
{
OnAuthenticationFailed = AuthenticationFailed
};
});
我得到的这个例子对 Core 1.1 和 Core 2.0 都有效,请添加下面的 Oath 身份验证,
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddAzureAdB2C(options => Configuration.Bind("Authentication:AzureAdB2C", options))
您的配置选项将在 class "AzureAdB2CAuthenticationBuilderExtensions" 中定义,它位于
azure b2c project
看起来你的令牌没有从 Azure 更新它,你能从你的网络应用程序中获取令牌吗?您能否验证您没有得到 null
您是否在您的 azure b2c 租户网络应用程序上注册了您的 api 范围?
"ApiScopes":“https://fabrikamb2c.onmicrosoft.com/demoapi/demo.read”
您必须在您的网络中设置范围 api 并允许在网络应用程序上阅读,请点击 link 以进行设置
我有一个用于测试的现有小应用程序,它在 Web 应用程序和 API 的 Asp.Net Core 1.1 中,身份验证是使用 Azure AD B2C 完成的。 我正在尝试将它移动到 .Net Core 2.0,但我不知道如何让它工作,我尝试使用 GitHub Azure-Samples for Web App 和 API 中的两个示例,但我有尝试访问 api 时出现未经授权或 500 错误,如果您有一个使用 2.0 并受 AD B2C 保护的 Web 应用程序调用 Web api 的工作示例,我们将不胜感激。
编辑: 我用来测试的样本是: 网络应用程序:WebApp-OpenIDConnect-DotNet core2.0 网站 Api:B2C-WebApi core2.0 ,我更改了 appsettings 值以匹配我的 b2c 目录。
对于我的 asp.net 核心 1.1 测试应用程序,我使用与上面相同的示例,但来自 master 分支,appsettings 具有相同的值。
编辑 2 默认情况下,在 startup.cs 我有这个:
services.AddAuthentication()
.AddJwtBearer(option => new JwtBearerOptions
{
Authority = string.Format("https://login.microsoftonline.com/tfp/{0}/{1}/v2.0/",
Configuration["Authentication:AzureAd:Tenant"], Configuration["Authentication:AzureAd:Policy"]),
Audience = Configuration["Authentication:AzureAd:ClientId"],
Events = new JwtBearerEvents
{
OnAuthenticationFailed = AuthenticationFailed
}
});
这给了我以下错误:
Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求开始 HTTP/1.1 GET http://localhost:44352/api/values/5
Microsoft.AspNetCore.Server.Kestrel:错误:连接 ID“0HL89JHF4VBLM”,请求 ID“0HL89JHF4VBLM:00000001”:应用程序引发了未处理的异常。
System.InvalidOperationException: 未指定 authenticationScheme,也未找到 DefaultChallengeScheme。
如果这样修改services.AddAuthentication
services.AddAuthentication(sharedOption =>
{
sharedOption.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
现在的错误是
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:信息:验证令牌 xxx 失败。 Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException:IDX10500:签名验证失败。没有提供安全密钥来验证签名。 在 System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(字符串令牌,TokenValidationParameters validationParameters) 在 System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(字符串令牌、TokenValidationParameters validationParameters、SecurityToken& validatedToken) 在 Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.d__6.MoveNext()
我在样本上看到了一个拉取请求,它纠正了这个问题 (Link),services.AddAuthentication 必须更改为:
services.AddAuthentication(options =>
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(jwtOptions =>
{
jwtOptions.Authority = $"https://login.microsoftonline.com/tfp/{Configuration["Authentication:AzureAd:Tenant"]}/{Configuration["Authentication:AzureAd:Policy"]}/v2.0/";
jwtOptions.Audience = Configuration["Authentication:AzureAd:ClientId"];
jwtOptions.Events = new JwtBearerEvents
{
OnAuthenticationFailed = AuthenticationFailed
};
});
我得到的这个例子对 Core 1.1 和 Core 2.0 都有效,请添加下面的 Oath 身份验证,
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddAzureAdB2C(options => Configuration.Bind("Authentication:AzureAdB2C", options))
您的配置选项将在 class "AzureAdB2CAuthenticationBuilderExtensions" 中定义,它位于 azure b2c project
看起来你的令牌没有从 Azure 更新它,你能从你的网络应用程序中获取令牌吗?您能否验证您没有得到 null
您是否在您的 azure b2c 租户网络应用程序上注册了您的 api 范围? "ApiScopes":“https://fabrikamb2c.onmicrosoft.com/demoapi/demo.read”
您必须在您的网络中设置范围 api 并允许在网络应用程序上阅读,请点击 link 以进行设置