mvc windows身份验证允许来自域的所有用户

lau*_*ben 3 .net vb.net authentication asp.net-mvc-3

我有一个使用Windows身份验证的mvc Intranet应用程序.它目前有一个控制器有三个动作.

第一个动作(索引)应该对每个人都可用,这没问题.第二个和第三个操作应仅对特定DOMAIN中的用户可用.但是<Authorize()>标签只给我2个选项:角色或用户.我尝试使用用户并将其设置为'DOMAIN*'和'DOMAIN \?' 但这不起作用.

我一直在互联网上搜索,但似乎找不到任何方法来完成我想要的东西.我希望有人能帮助我!

谢谢

jru*_*ell 10

使用DOMAIN\Domain Users的角色名.它是一个内置的组,包含,你猜对了,域中的所有用户.


Bra*_*tie 5

添加到jrummel提到的内容,使用以下内容装饰您的控制器或操作:

[Authorize(Roles = "DOMAIN\Domain Users")]
Run Code Online (Sandbox Code Playgroud)

这将只允许具有特定角色的用户(在此特定域的用户中)访问控制器/操作(取决于您装饰的用户).或者,您可以为域的目的创建自己的授权属性:

/// <summary>
/// Specified which domains a user should belong to in order to access the decorated
/// controller/action
/// </summary>
public class DomainAuthorizeAttribute : AuthorizeAttribute
{
    private String[] domains = new String[0];

    /// <summary>
    /// List of acceptable domains
    /// </summary>
    public String[] Domains
    {
        get { return this.domains; }
        set { this.domains = value; }
    }

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

        // User not logged in
        if (!httpContext.User.Identity.IsAuthenticated)
        {
            return false;
        }

        // No roles to check against
        if (this.Domains.Length == 0)
        {
            return true;
        }

        // check if they're on any of the domains specified
        String[] roles = this.Domains.Select(d => String.Format(@"{0}\Domain Users", d)).ToArray();
        if (roles.Any(httpContext.User.IsInRole))
        {
            return true;
        }

        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

这样的事情应该允许你这样做:
[DomainAuthorize(Domains = new[]{ "DOMAIN1", "DOMAIN2" })]