.NET 核心 MVC 5 Windows 身份验证

.NET Core MVC 5 Windows Authentication

我想在我的 .NET Core MVC 5 网络应用程序上使用 Windows 身份验证。允许特定域用户和 AD 组。

我在根部添加了一个web.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <authentication mode="Windows" />
    <authorization>
      <allow users="mydomain\username1, mydomain\username2" />
      <deny users="?" />
    </authorization>
    <roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider">
      <providers>
        <clear />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

我将此添加到 startup.cs

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

我在我的控制器中添加了这个

[Authorize(Roles = @"mydomain\ADgroupName")]
[Authorize(Users = @"mydomain\username1")]
public class HomeController : Controller

在项目属性中,我禁用了匿名身份验证并启用了 Windows 身份验证。

我添加了对 Microsoft.AspNetCore.Authentication 的项目引用。

完成所有这些后,我收到错误“找不到类型或名称空间 'Users'”。

我在这里错过了什么?

the error "Type or namespace 'Users' could not be found".

AuthorizeAttribute Class可以看出,Authorize属性只有角色和策略属性,没有用户属性,所以会显示以上错误。

如果要为指定用户设置授权权限,可以为用户创建策略,然后在Controller中设置Authorize属性如下:

 [Authorize(Policy = "policyforUser")]
 public class HomeController : Controller

有关创建策略的更多详细信息,请参阅以下链接:

Policy-based authorization in ASP.NET Core

ASP.NET Core - Authorization Using Windows Authentication

Using AD groups to authorise access to pages using IIS Windows Authentication