简体   繁体   English

ASP.NET 核心 3.1 - 访问被拒绝

[英]ASP.NET Core 3.1 - Access denied

I'm quite new in ASP.NET Core 3.1 Razor Pages and I have a question.我在 ASP.NET Core 3.1 Razor 页面中很新,我有一个问题。 Hopefully you can help me further:).希望你能帮我更多:)。

What I want to have is an application with Windows AD Security.我想要的是一个带有 Windows AD Security 的应用程序。 Description of what I want to do:我想做的事情的描述:

  • Customer needs to login using his/her AD account.客户需要使用他/她的 AD 帐户登录。
  • The user is authorized if entered a valid AD account/password combination.如果输入有效的 AD 帐户/密码组合,则用户被授权。
  • The user have rights to see/adjust specific pages if in a specific group, let's say if in the Administrators group of the server where the application is running on.如果在特定组中,用户有权查看/调整特定页面,假设是否在运行应用程序的服务器的管理员组中。

The problem that I have is the following.我遇到的问题如下。 In LaunchSettings.json I have placed this code:在 LaunchSettings.json 我放置了以下代码:

    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
      "applicationUrl": "http://localhost:65385",
      "sslPort": 44356
    }
  } 

Then in Startup.cs I have added AddAuthentication.然后在 Startup.cs 我添加了 AddAuthentication。

    public void ConfigureServices(IServiceCollection services)
    {
      services.AddAuthentication(IISDefaults.AuthenticationScheme);
      services.AddRazorPages();
    }

And in the Configure part:在配置部分:

      app.UseAuthentication();
      app.UseAuthorization();

Then finally I created a separate folder, called Admin, in my Pages folder.最后,我在我的 Pages 文件夹中创建了一个名为 Admin 的单独文件夹。 I want to restrict this folder for only the Administrators group.我想将此文件夹限制为仅管理员组。 So I added the Authorize to the Index1Model.所以我将 Authorize 添加到 Index1Model。

  [Authorize(Roles = "Administrators")]
  public class Index1Model : PageModel
    {
        public void OnGet()
        {
        }
    }

Launching this code locally with IIS Express and clicking the page protected I do get the following error:使用 IIS Express 在本地启动此代码并单击受保护的页面,我确实收到以下错误:

Access denied

I thought it might have to do with impersonation.我认为这可能与模仿有关。 But when I enable this in IIS then I cannot open the application anymore.但是当我在 IIS 中启用它时,我就无法再打开应用程序了。 The user which is display in the upper corner of my program is in the Administrator group and therewith should be allowed to see the page.显示在我的程序右上角的用户属于管理员组,因此应该允许查看该页面。 What am I overlooking?我在看什么? Thanks for helping me out!谢谢你的协助!

Have you enabled windows authentication in IIS?您是否在 IIS 中启用了 windows 身份验证? If not try that, else allow anonymous authentication and somewhere on your page display the user and it's roles so you can see what identity is flowing through on IIS.如果不尝试,则允许匿名身份验证,并在页面上的某处显示用户及其角色,以便您可以看到 IIS 上流经的身份。 You might have to change the identity that your app pool is running under but I am sure this has something to do with your IIS configuration.您可能需要更改运行应用程序池的标识,但我确信这与您的 IIS 配置有关。

As far as I know, the windows authentication will just check the the user is authenticated or not.据我所知,windows 身份验证只会检查用户是否经过身份验证。 It will not provide any role based control in the MVC application.它不会在 MVC 应用程序中提供任何基于角色的控制。

So your Authorize attribute will be useless.因此,您的 Authorize 属性将毫无用处。

To achive AD role based authorize, I suggest you could consider using Policy-based authorization to authenticate only users from a Active Directory group have access to the page.为了实现基于 AD 角色的授权,我建议您可以考虑使用基于策略的授权来验证只有来自 Active Directory 组的用户才能访问该页面。 Detials, you could refer to article .细节,可以参考文章

You could create a custom Policy Authorization handlers to check User's all ADGroups and check if they contains your desired group name.您可以创建自定义策略授权处理程序来检查用户的所有 ADGroups 并检查它们是否包含您想要的组名。

More details, you could refer to below steps:更多细节,您可以参考以下步骤:

1.Create CheckADGroupRequirement(accept a parameter) 1.创建CheckADGroupRequirement(接受一个参数)

public class CheckADGroupRequirement : IAuthorizationRequirement
    {
        public string GroupName { get; private set; }

        public CheckADGroupRequirement(string groupName)
        {
            GroupName = groupName;
        }
    }

2.Create Handler 2.创建处理程序

public class CheckADGroupHandler : AuthorizationHandler<CheckADGroupRequirement>
    {
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
                                                       CheckADGroupRequirement requirement)
        {
            //var isAuthorized = context.User.IsInRole(requirement.GroupName);

            var groups = new List<string>();//save all your groups' name
            var wi = (WindowsIdentity)context.User.Identity;
            if (wi.Groups != null)
            {
                foreach (var group in wi.Groups)
                {
                    try
                    {
                        groups.Add(group.Translate(typeof(NTAccount)).ToString());
                    }
                    catch (Exception e)
                    {
                        // ignored
                    }
                }
               if(groups.Contains(requirement.GroupName))//do the check
                {
                    context.Succeed(requirement);
                }
            }

            return Task.CompletedTask;
        }
    }

3.Register Handler in ConfigureServices 3.在ConfigureServices中注册Handler

services.AddAuthorization(options =>
{
    options.AddPolicy("ADRoleOnly", policy =>
        policy.Requirements.Add(new CheckADGroupRequirement("DOMAIN\\Domain Admin")));
});

services.AddSingleton<IAuthorizationHandler, CheckADGroupHandler>();

4.Controller 4.Controller

[Authorize(Policy = "ADRoleOnly")]
 public class ADController : Controller

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM