【问题标题】:.Net Core authorize authenticated users with Azure AD.Net Core 使用 Azure AD 授权经过身份验证的用户
【发布时间】:2018-09-24 16:16:33
【问题描述】:

嗨,

我们有一个带有 Azure AD 身份验证的 .Net Core 2.0 应用。 目前,我们有一些与该应用关联的用户在 Azure AD 中被赋予了该应用的角色,并且正在使用策略来检查授权。由于业务需求,我们希望将应用开放给公司的任何用户,所以我们只需要检查用户是否经过身份验证即可。

作为一名经验丰富的 C# 开发人员,我去了具有以下代码的控制器


[Authorize(Policy = PolicyNames.RequireLinecardsUser)]
        public class LinecardController : Controller
        {
            //controller code here
        }
并将其更改为

    [Authorize]
        public class LinecardController : Controller
        {
            //controller code here
        }
对于可能仍然具有 LinecardUser 角色的用户来说,它可以工作。它可以访问应用程序(主入口点在该控制器内)。但是,当我们使用在应用程序中没有角色的用户对其进行测试时,它会收到拒绝访问异常。

所以下一步我们开始启动,并删除了授权配置(它只有 2 个现在未使用的策略)。


    public void ConfigureServices(IServiceCollection services)
            {
                try
                {
                    services.AddDbContext(options =>
                    {
                        options.UseSqlServer(Configuration.GetConnectionString("LinecardsContext"), opt => opt.UseRowNumberForPaging());
                    });
                    services.AddAuthentication(options =>
                        {
                            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                            options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
                            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                            options.DefaultForbidScheme = OpenIdConnectDefaults.AuthenticationScheme;
                        })
                        .AddCookie(options =>
                        {
                            options.AccessDeniedPath = "/Account/AccessDenied/";
                            options.LoginPath = "/Account/Login/";
                        })
                        .AddOpenIdConnect(options =>
                        {
                            configuration.GetSection("AzureAd").Bind(options);
                        });

                   services.AddAuthorization(options =>
                        {
                            options.AddPolicy(PolicyNames.RequireLinecardsUser,
                                      policy =>
                                      {
                                              policy.AddRequirements(new LinecardsWebUserRequirement()); 
                                              policy.RequireAuthenticatedUser(); // Adds DenyAnonymousAuthorizationRequirement 
                                                                                 // By adding the CookieAuthenticationDefaults.AuthenticationScheme, if an authenticated
                                                                                 // user is not in the appropriate role, they will be redirected to a "forbidden" page.
                                              policy.AddAuthenticationSchemes(CookieAuthenticationDefaults.AuthenticationScheme);
                                      });

                            options.AddPolicy(PolicyNames.RequireLinecardsAdmin,
                                        policy =>
                                        {
                                            policy.AddRequirements(new LinecardsWebAdminRequirement());
                                            policy.RequireAuthenticatedUser(); // Adds DenyAnonymousAuthorizationRequirement 
                                                                               // By adding the CookieAuthenticationDefaults.AuthenticationScheme, if an authenticated
                                                                               // user is not in the appropriate role, they will be redirected to a "forbidden" page.
                                                policy.AddAuthenticationSchemes(CookieAuthenticationDefaults.AuthenticationScheme);
                                        });
                        });

                    services.AddScoped();

                    services.RegisterTypes();

                    services.AddReact();

                    services.AddAutoMapper();

                    services.Configure(x => x.ValueCountLimit = 100000);
                    services.AddMvc();
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Error happened on configuring services");
                    throw;
                }
            }
这意味着在前面的代码中我们删除了 services.AddAuthentication();堵塞。结果相同,具有角色的用户可以访问应用程序,而没有角色的用户则不能。 (也尝试在匿名模式下使用浏览器,以确保此处没有缓存问题)。

最后我们尝试更改 RequireLinecardsUser 策略,所以它没有策略。AddRequirements(new LinecardsWebUserRequirement());行(并将控制器更改为带有策略的原始授权行。 再一次,同样的结果,对我来说很好,没有角色的用户访问被拒绝。

我错过了一些明显的东西吗? Core 2.0 中发生了什么变化? Azure 相关的东西? 因为我发现的每个文档都说不带任何参数的 [Authorize] 注释应该可以正常工作,并且只在允许代码继续之前验证用户是否经过身份验证......

你们中的一些人可能已经注意到该应用程序有一个我没有接触过的反应前端,请随时指出可能与此相关的任何问题。

PPS:很抱歉,如果格式不符合标准,但这是我的第一个问题

【问题讨论】:

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


    【解决方案1】:

    在使用此应用程序进行更多测试后,异常指出 Azure 是造成此问题的罪魁祸首。 在 Azure 的应用程序属性上,有一个选项询问“需要用户分配吗?”。该应用程序的特定设置设置为是。 将其更改为 no 即可解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-19
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多