简体   繁体   English

Azure B2C 和 .net 核心 3.1 认证问题

[英]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.

相关问题 将 Azure B2C 与 MVC、.NET Core 3.1 结合使用 - Using Azure B2C with MVC, .NET Core 3.1 .NET Core API Azure B2C认证配置 - .NET Core API Azure B2C authentication configuration .Net Core 3.1 - 如何为 Azure AD B2C 生成注册链接? - .Net Core 3.1 - how to generate Sign Up link for Azure AD B2C? 使用 Azure AD B2C on.Net Core 3.1 获取 OBO 流的错误 AADST50013 - Getting error AADST50013 for OBO flow using Azure AD B2C on .Net Core 3.1 ASP.NET Core 3.1 如何使用 Azure AD B2C 返回 401 Unauthorized 而不是 Challenge - ASP.NET Core 3.1 how to return 401 Unauthorized instead of Challenge with Azure AD B2C 尝试使用 React.js 和 Redux 将 Azure B2c 身份验证添加到 Asp.net 核心 - trying to add Azure B2c authentication to Asp.net core with React.js and Redux 如何从 Azure AD B2C 用户商店获取 ASP.NET Core 3.1 中的用户信息? - How to get user information in ASP.NET Core 3.1 from Azure AD B2C user store? ASP.NET Core 3.1 和 Azure AD B2C 与外部身份提供者身份验证 - invalid_request - ASP.NET Core 3.1 and Azure AD B2C with external Identity Providers auth - invalid_request Azure B2C 检查用户 email Id 是否存在或不使用 ASP.NET Core 3.1 MVC 中的图表 - Azure B2C check user email Id exists or not using Graph in ASP.NET Core 3.1 MVC 具有多个 B2C 环境的基于 .NET Core 路由的身份验证 - .NET Core route based authentication with multiple B2C environments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM