Wednesday, April 13, 2011

Trick: Roles in ASP.NET and Windows Authentication

Hi Folks,

I have been reading about how to enable Roles in a Windows Authentication in an ASP.NET Web Application, and then I came throught this trick which I wanted to share with you, here it goes:

One tip/trick I like to use is to take advantage of the “Application_Start” event handler within Global.asax to setup my Roles if they don’t already exist, and map any initial users into them if necessary. To-do this, choose File - Add New Item and select the “Global.asax” file item. Then add this code to your Application_Start event handler:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
If (Roles.RoleExists("Auditors") = False) Then
    Roles.CreateRole("Auditors")
End If
If (Roles.RoleExists("Approvers") = False) Then
    Roles.CreateRole("Approvers")
End If
If (Roles.RoleExists("Admins") = False) Then
    Roles.CreateRole("Admins")
    Roles.AddUserToRole("YOURCOMPANY\yourusername", "Admins")
End If
End Sub

Or, you can check the Scott's full post in the following link:
Recipe: Implementing Role-Based Security with ASP.NET 2.0 using Windows Authentication and SQL Server


Happy programming!
Saed

No comments:

Post a Comment