I am working on a simple ASP.NET Intranet with Windows Authentication. I am authenticating users with Active Directory to allow them access to the site, but would like to implement site-specific user roles to manage page & content access without disturbing or adding to the Windows User Groups already in place.
My thought on how to accomplish this was to create a UserRoles
table, and place a specific RoleId
in the Users
table for each user. Then I would compare the Currently Logged In
username with the Users
table to find out which role the user is in.
Question 1:
Is this an OK way to go about this?
Question 2 (If the answer to question 1 is yes):
How can I select the correct RoleId for the current user, and use in an IF statement?
This is what I was thinking:
var db = Database.Open("Database") ;
// Find out what my Windows Username is
var currentUser = Request.LogonUserIdentity.Name.Substring(Request.LogonUserIdentity.Name.LastIndexOf(@"\") + 1);
// Find out what Role I'm in
var getUser = "SELECT RoleId FROM Users WHERE UserID = @0";
// Get the result
var selectedUser = db.Query(getUser, currentUser);
var requiredRole = "1";
// Choose where I should go
if (SelectedRoleId != requiredRole)
{
Response.Redirect("~/AreaRestricted");
}
Any help would be great.