Saed Al Shaar Dev Tips and Tricks

.NET and SharePoint from a different angle!

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

Advertisements

Single Post Navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: