[英]Azure B2C and .net core 3.1 Authentication Problem
I am upgrading my .net 2.0 core B2C app to .net 3.1.我正在将我的 .net 2.0 核心 B2C 应用程序升级到 .net 3.1。 I have followed this example , but I keep getting a 401 response when trying to access an endpoint with a valid jwt.
我已按照此示例进行操作,但在尝试使用有效 jwt 访问端点时,我不断收到 401 响应。 My app has been registered correctly in Azure B2C as making an authenticated request works against my .net core 2.0 app.
我的应用程序已在 Azure B2C 中正确注册,因为发出经过身份验证的请求适用于我的 .net 核心 2.0 应用程序。
Here's my code in Startup.cs:这是我在 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();
});
}
Here's my appSettings.json这是我的 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": "*"
}
Here is the authorization tag on my controller这是我的 controller 上的授权标签
[ApiController]
[Authorize]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
...
The endpoint I hit is working when auth is turned off, so the problem is not occurring as the controller is initialized or from a bad database connection.当身份验证关闭时,我点击的端点正在工作,因此问题不会发生,因为 controller 已初始化或来自错误的数据库连接。 Any insight would be appreciated.
任何见解将不胜感激。 Also there aren't very many .net core 3.1 b2c web api examples out there so any direction to working sample code would be helpful as well.
也没有很多 .net 核心 3.1 b2c web api 示例,因此任何工作示例代码的方向都会有所帮助。
So I downloaded the full example, put in my b2c properties and it worked.所以我下载了完整的示例,放入我的 b2c 属性,它就可以工作了。 I tried moving piece by piece the exact code from the example into my working solution, and it didn't work.
我尝试将示例中的确切代码逐个移动到我的工作解决方案中,但它没有用。 I remember installing graphql, experimenting with it, and uninstalling it, so maybe the dependencies broke some how.
我记得安装 graphql,试验它,然后卸载它,所以也许依赖关系破坏了一些方式。 I cleaned the project and rebuilt and that didn't work, but maybe something was installed / uninstalled incorrectly.
我清理了项目并重新构建,但没有成功,但可能安装/卸载不正确。 I created a new project, moved all of my code from my working solutoin over, and it worked.
我创建了一个新项目,将我所有的代码从我的工作解决方案中移过来,并且它工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.