无法验证来自 Domain\Users 组的用户

Unable to Authenticate Users From Domain\Users Group

我正在尝试为我的 MVC 应用程序验证域中的所有用户。目前我正在与用户 PC_NAME/Administrator.

进行测试

我已尝试按照此 answer.

中的建议,在我的控制器 class 中从 PC_NAME 授权用户组
[Authorize(Roles = "PC_NAME\Domain Users")]

这不行,我只是被浏览器提示登录。

我也在 web.config 中尝试过

<authorization>
      <allow roles="PC_NAME\Domain Users"/>
      <deny users="*"/>
</authorization>

这个也不成功


郑重声明,我尝试只验证用户角色而不指定域,并且我能够访问我的网站

[Authorize(Roles = "Users")]

当我只指定一个用户名时它也有效

[Authorize(User = "PC_NAME\Administrator")]

如何验证来自单个域(在本例中为 VSD-PROMETHEUS)的所有用户?

我通过创建自定义授权属性实现了此功能。

using System;
using System.Web;
using System.Web.Mvc;

/// <summary>
/// Authorises User based on what domain they are on.
/// </summary>
public class AuthorizeDomainAttribute : AuthorizeAttribute
{
    /// <summary>
    /// List of domains to authorise
    /// </summary>
    public string[] Domains { get; set; }

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext == null)
        {
            throw new ArgumentNullException("httpContext");
        }

        // Get the domain part of the username
        string userDomain = httpContext.User.Identity.Name.Substring(0, httpContext.User.Identity.Name.LastIndexOf('\'));

        // Check if the user is on any of the domains specified
        foreach(string domain in this.Domains)
        {
            if (userDomain == domain)
            {
                return true;
            }
        }

        // Otherwise don't authenticate them
        return false;
    }
}

然后在我的控制器上使用这个属性。

[AuthorizeDomain(Domains = new[] { "PC_NAME")]