【发布时间】:2021-05-16 07:22:27
【问题描述】:
我正在将我的 .net 2.0 核心 B2C 应用程序升级到 .net 3.1。我已关注this example,但在尝试使用有效 jwt 访问端点时,我不断收到 401 响应。我的应用已在 Azure B2C 中正确注册,因为发出经过身份验证的请求适用于我的 .net core 2.0 应用。
这是我在 Startup.cs 中的代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
builder =>
{
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
});
});
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(options =>
{
Configuration.Bind("AzureAdB2C", options);
options.TokenValidationParameters.NameClaimType = "name";
},
options => { Configuration.Bind("AzureAdB2C", options); });
services.AddControllers();
services.AddTransient<IProjectRepo, ProjectRepo>();
services.AddAuthorization(options =>
{
// Create policy to check for the scope 'read'
options.AddPolicy("ReadScope",
policy => policy.Requirements.Add(new ScopesRequirement("read")));
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(MyAllowSpecificOrigins);
app.UseAuthorization();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
这是我的 appSettings.json
{
"AzureAdB2C": {
"Instance": "https://<my-tenant>.b2clogin.com/tfp/",
"ClientId": "xxxxx-xxx-x-xxxx-xxxxxxxxx",
"Domain": "<my-tenant>.onmicrosoft.com",
"TenantId": "yyyyyy-yyyyy-yyyy-yyyyyyyy",
"SignUpSignInPolicyId": "B2C_1_DefaultSignInSignUp"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
这是我的控制器上的授权标签
[ApiController]
[Authorize]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
...
当身份验证关闭时,我点击的端点正在工作,因此在控制器初始化或数据库连接错误时不会出现问题。任何见解将不胜感激。此外,没有太多 .net core 3.1 b2c web api 示例,因此任何工作示例代码的指导也会有所帮助。
【问题讨论】:
-
除了迁移到核心 3.1 之外,您是否更改了代码中的某些内容
-
是的,我正在升级到具有不同语法和结构的 System.Identity.Web。我认为我没有正确使用它。我很确定我的 b2c 应用程序设置属性是正确的,并且我有一个正常工作的 jwt。我正在努力解决这个问题。
标签: c# .net-core azure-active-directory azure-ad-b2c