June 2008
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          
Search

 
Catagories
Archives
Recent Entries
Links
RSS
roles using windows authentication
Catagory: programming · This Entry · Comment(0) · eMail entry · Google
April 26, 2004 03:04 PM

programming

There's a similar approach with windows authentication, as with forms authentication. You still have to manually assign the users/roles in WindowsAuthentication_Authenticate to get the full benefit of user and role verification using parameter attributes - or web.config user/role declarations.

Here's the code (in vb) to map the values in...


[this example code uses a custom class with the function GenericPrincipal(e.Identity, roleStrng) to create the User. It's just a thin class containing the user name, and role list.]

Sub WindowsAuthentication_Authenticate(ByVal sender As Object, ByVal e As WindowsAuthenticationEventArgs)
  Dim roleStrng() As String = GetUserRoles()
  e.User = New GenericPrincipal(e.Identity, roleStrng)End Sub

Private Function GetUserRoles() As String()
  Dim myDomain As AppDomain = Thread.GetDomain()
  myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
  Dim al As New ArrayList
  Dim myPrincipal As WindowsPrincipal = CType(Thread.CurrentPrincipal, WindowsPrincipal)
  Dim wbirFields As Array = [Enum].GetValues(GetType(WindowsBuiltInRole))
  Dim roleName As Object
  For Each roleName In wbirFields
    Try
      If myPrincipal.IsInRole(CType(roleName, WindowsBuiltInRole))
      Then
        al.Add(roleName.ToString())
      End If
    Catch
    End Try
  Next roleName
  Return CType(al.ToArray(GetType(String)), String())
End Function


[note, users and roles belong to domains in windows. so you may need to explicitly use the "domain\account" syntax. e.g. "domain\user" or "domain\role"]





Comments

Post a comment
Name:


Email Address:


URL:


Comments: