【问题标题】:AspCore 2.2 Azure Active Directory : Authorization failedAsp Core 2.2 Azure Active Directory:授权失败
【发布时间】:2019-09-16 19:33:30
【问题描述】:

我尝试集成 Azure Active Directory 和 Asp.net CORE 2.2。

public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
            .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseAuthentication();
        app.UseMvc();
    }

Appsettings.json

{"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "xxxxx.onmicrosoft.com",
"TenantId": "xxxxxx", 
"ClientId": "xxxx" } },"AllowedHosts": "*"}

结果: 错误:信息:Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2] 授权失败。

拜托,我找到了任何解决此问题的方法。

非常感谢

【问题讨论】:

  • 我不知道这是否是您的情况,但如果您尝试使用 MS Graph 的令牌进行身份验证,它将无法正常工作。您需要为自己的 Web API 颁发令牌,而不是重复使用来自其他资源的令牌。

标签: azure asp.net-core azure-active-directory


【解决方案1】:

可能一个很好的解决办法是修改startup.cs

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(sharedOptions =>
        {
            sharedOptions.DefaultChallengeScheme = AzureADDefaults.AuthenticationScheme;
            sharedOptions.DefaultAuthenticateScheme = AzureADDefaults.AuthenticationScheme;
        })
        .AddAzureAD(options => Configuration.Bind("AzureAD", options));

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

并包含在控制器中

[Authorize(AuthenticationSchemes = "AzureAD")]
[Route("api/[controller]")]
[ApiController]

【讨论】:

    【解决方案2】:

    AddAzureADBearer 将 JWT 不记名身份验证添加到您的 Azure Active Directory 应用程序的应用程序。它通常用于使用 AAD 令牌保护您的应用程序。如果那是您的情况,请检查 Authorization failed 的详细错误消息。您可以参考以下链接获取代码示例:

    https://stackoverflow.com/a/57619013/5751404

    另一个场景是你想添加 Azure AD 登录验证。最简单的方法是使用默认的 Azure AD 模板:Change Authentication --> Work or School Accounts。或者手动添加Microsoft.AspNetCore.Authentication.AzureAD.UI包并使用AddAzureAD扩展:

    https://stackoverflow.com/a/54546245/5751404

    【讨论】:

    • 我尝试了你最简单的方法,但它对我不起作用。 :(
    • 显示细节,不只是doesn't work ,是什么场景,什么是错误信息
    • 好的,我从 Visual Studio 2019 生成了解决方案。 > 工作帐户 > 我的租户在这里,但是当我编译代码时,身份验证不起作用。所以可能在 VS 模板中他们遗漏了一些东西......
    • 认证失败。默认情况下,控制器使用 [Authorize]。就我而言,我通过 [Authorize(AuthenticationSchemes = "AzureAD")] 更改 [Authorize] 并且它有效! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 2017-03-11
    • 2020-05-17
    • 2023-03-25
    • 1970-01-01
    • 2019-07-31
    • 2017-10-12
    相关资源
    最近更新 更多