提问者:小点点

Asp.NetCore 2.0和AzureADB2C在WebApp和API上进行身份验证


我有一个现有的小应用程序,我用于测试,它是在Asp.Net核心1.1的Web应用程序和API,身份验证是使用AzureADB2C完成的。我正试图将其移动到。Net Core 2.0,但我无法弄清楚如何让它工作,我尝试使用GitHub Azure-Samples的Web应用程序和API的示例,但我有一个未经授权或500错误时尝试访问api,如果你有一个工作示例,从使用2.0的Web应用程序调用Web api,并受ADB2C的保护,将不胜感激。

编辑:我用来测试的示例是:Web App:WebApp-OpenIDConnect-DotNet core2.0 Web Api:B2C-WebApi core2.0,我更改了appsettings值以匹配我的b2c目录。

对于我的asp.netcore 1.1测试应用程序,我使用与上述相同的示例,但来自master分支,appsettings的值相同。

默认编辑2,在start. 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.内部. WebHost:信息:请求起始HTTP/1.1 GEThttp://localhost:44352/api/values/5
Microsoft.AspNetCore.Server.Kestrel:错误:连接id"0HL89JHF4VBLM",请求id"0HL89JHF4VBLM:00000001":应用程序抛出未处理的异常。System.InvalidOperationException:未指定身份验证方案,也未找到DefaultChallengeSchem。

如果修改了服务。像这样的身份验证

        services.AddAuthentication(sharedOption =>
        {
            sharedOption.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
        })

错误是现在

Microsoft. AspNetCore.Authentication.JwtBearer.JwtBearerHandler:信息:验证令牌xxx失败。Microsoft.IdtyModel.Tokens.SecurityTokenInvalidSignatureException:IDX10500:签名验证失败。没有提供安全密钥来验证签名。在System.IdtyModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(String token,TokenValidationParameters validationParameters)在System.IdtyModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(String token,TokenValidationParameters validationParameters,SecurityToken


共2个答案

匿名用户

我在纠正此问题的示例上看到了一个拉取请求(链接),服务。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 Authentication,

services.AddAuthentication(sharedOptions =>
            {
                sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddAzureAdB2C(options => Configuration.Bind("Authentication:AzureAdB2C", options))

您的配置选项将在类“AzureAdB2CAuthenticationBuilderEx的情”中定义,该类位于azure b2c项目中

看起来你的令牌没有从Azure更新,你能从你的Web应用程序中获取令牌吗?你能验证你没有得到空吗

您是否在azure b2c租户Web应用程序上注册了api范围?"ApiScopes":"https://fabrikamb2c.onmicrosoft.com/demoapi/demo.read"

您必须在Web api中设置范围并允许在Web应用程序上读取,请点击链接进行设置