【问题标题】:Azure B2C and .net core 3.1 Authentication ProblemAzure B2C 和 .net core 3.1 身份验证问题
【发布时间】: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


【解决方案1】:

所以我下载了完整的示例,放入我的 b2c 属性,它就可以工作了。我尝试将示例中的确切代码逐个移动到我的工作解决方案中,但它没有用。我记得安装了graphql,试验了它,然后卸载了它,所以也许依赖关系破坏了一些方法。我清理了项目并重新构建,但没有成功,但可能安装/卸载不正确。我创建了一个新项目,将我所有的代码从我的工作解决方案中移了过来,它工作了。

【讨论】:

    猜你喜欢
    • 2021-07-21
    • 2021-01-03
    • 1970-01-01
    • 2020-02-24
    • 2019-10-31
    • 2020-03-09
    • 1970-01-01
    • 2020-06-16
    • 2020-05-30
    相关资源
    最近更新 更多