3

我有一个使用Windows身份验证的mvc Intranet应用程序。它目前有一个控制器和三个动作。mvc windows身份验证允许来自域的所有用户

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

我一直在搜索整个互联网,但似乎无法找到任何方式来完成我想要的。我希望这里有人能帮助我!

感谢

回答

10

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

+0

就是这样,完美!非常感谢你。 – laureysruben

5

添加到jrummel提到什么,装点您的控制器或操作有以下:

[Authorize(Roles = "DOMAIN\Domain Users")] 

这将只允许在特定角色的用户(在此可以在特定域的用户)访问控制器/行动(取决于你装饰)。另外,您也可以创建自己的授权属性为域的目的:

/// <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; 
    } 
} 

类似的东西应该让你做的事:
[DomainAuthorize(Domains = new[]{ "DOMAIN1", "DOMAIN2" })]

+0

我知道该怎么做,只是不知道我可以使用“DOMAIN \ Domain Users”。有人在我的帖子中提到过,但显然被过滤掉了,可能是因为我用的是vb语法:< and > – laureysruben

+0

@laureysruben:确实没有看到属性(我的道歉)。如果你愿意的话,我还会写一个'[DomainAuthorize(Domains =“ABC,DEF”)]' –

+0

这是一个非常优雅的解决方案,我认为这种方式更加“明显”。我从来没有做过这样的事情(根据现有的属性制作自定义属性),很高兴看到它是如何完成的。谢谢! – laureysruben

2

对于有兴趣的人,这里是上面的VB版代码片段:

''' <summary> 
''' Specified which domains a user should belong to in order to access the decorated 
''' controller/action 
''' </summary> 
Public Class DomainAuthorizeAttribute 
    Inherits AuthorizeAttribute 
    Private m_domains As [String]() = New [String](-1) {} 

    ''' <summary> 
    ''' List of acceptable domains 
    ''' </summary> 
    Public Property Domains() As [String]() 
     Get 
      Return Me.m_domains 
     End Get 
     Set(value As [String]()) 
      Me.m_domains = value 
     End Set 
    End Property 

    Protected Overrides Function AuthorizeCore(httpContext As HttpContextBase) As Boolean 
     If httpContext Is Nothing Then 
      Throw New ArgumentNullException("httpContext") 
     End If 

     ' User not logged in 
     If Not httpContext.User.Identity.IsAuthenticated Then 
      Return False 
     End If 

     ' No roles to check against 
     If Me.Domains.Length = 0 Then 
      Return True 
     End If 

     ' check if they're on any of the domains specified 
     Dim roles As [String]() = Me.Domains.[Select](Function(d) [String].Format("{0}\Domain Users", d)).ToArray() 

     For Each r In roles 
      If httpContext.User.IsInRole(r) Then 
       Return True 
      End If 
     Next 

     Return False 
    End Function 
End Class 

希望这会对某人有所帮助! (所有功劳都归Brad Bradley)

+0

感谢您的转换! –

相关问题