【问题标题】:Unable to Authenticate Users From Domain\Users Group无法验证来自域\用户组的用户
【发布时间】:2019-05-30 16:23:33
【问题描述】:

我正在尝试对我的 MVC 应用程序的域中的所有用户进行身份验证。目前我正在使用用户 PC_NAME/Administrator 进行测试。

我已尝试按照answer 中的建议在我的控制器类中从 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)?

【问题讨论】:

  • "这不起作用,我只是被浏览器提示登录。"好吧,要授权您使用该角色,它需要对您进行身份验证,那么它为什么不提示您登录呢?
  • 我希望只有在初始身份验证失败时才会提示您输入用户名/密码。比如上面提到的对没有域的角色用户进行授权时,我可以在没有提示的情况下成功登录。

标签: c# asp.net-mvc windows-authentication


【解决方案1】:

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

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")]

【讨论】:

    猜你喜欢
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多