Anton Margo
September 8th, 2004, 05:12 PM
Dear Professionals, I need your help because I am stuck.
I am trying to implement a role based ASP.NET authentication model with a single login page. I have several account types which should be redirected to a unique main page (for each account). Where if user is admin he will put his username and password and he should be verified and redirected to the adminmain.aspx, if other type then redirect should be othertypemain.aspx.
I am using the article
http://aspnet.4guysfromrolla.com/articles/082703-1.2.aspx
to implement the asp form authentication, and asp.net redirectfromlogin method to automatically create a session for any user who already logged in.
My login button has code:
Sub Submit_OnClick(sender as Object, e as EventArgs)
If Authenticate (txtUserName.Text, txtPassword.Text) Then
'Determine this user's status
Dim type as String = Getusertype(txtUserName.Text, txtPassword.Text)
Dim roleList As New ArrayList
roleList.Add("type")
Dim roleListArray As String() = roleList.ToArray(GetType(String))
HttpContext.Current.User = _
New System.Security.Principal.GenericPrincipal(User.Identity, roleListArray)
FormsAuthentication.RedirectFromLoginPage (txtUserName.Text, True)
Else
' Invalid credentials supplied, display message
Response.Write ("Invalid login credentials")
End If
End Sub
Function Authenticate checks if username and pwd are valid.
Function Getusertype checks the database and returns a string with account type.
Then each page will check if the user belongs to a certain group or not:
If User.IsInRole("Administrator") then
' Display sensitive material
End If
PROBLEM :
1)
Since I am using ASP.NET form authentication, I can not set the specific redirect page based on the type of the user account that i get from function Getusertype. *
FormsAuthentication.RedirectFromLoginPage (txtUserName.Text, True)
- this only takes username.
And I would want the code to create a session and redirect to the page I need (based on the account type). This way i will simple use code below to check if the user can see the xxxmain.aspx or not.
If User.IsInRole("xxx") then
' Display sensitive material
End If
*I believe there is a way to select a redirect page for RedirectFromLoginPage method by setting returnURL in the URL string, however I dont really want to post back to a login page and then redirect. Is there any nicer way?
2) I hope I am creating a role correctly:
'Determine this user's status
Dim type as String = getusertype(txtUserName.Text, txtPassword.Text)
Dim roleList As New ArrayList
roleList.Add("Admin")
Dim roleListArray As String() = roleList.ToArray(GetType(String))
HttpContext.Current.User = _
New System.Security.Principal.GenericPrincipal(User.Identity, roleListArray)
Please let me know if I am missing something.
Thanks a lot for all the useful comments,
I appreciate any help or links on the matter.
A.M.
I am trying to implement a role based ASP.NET authentication model with a single login page. I have several account types which should be redirected to a unique main page (for each account). Where if user is admin he will put his username and password and he should be verified and redirected to the adminmain.aspx, if other type then redirect should be othertypemain.aspx.
I am using the article
http://aspnet.4guysfromrolla.com/articles/082703-1.2.aspx
to implement the asp form authentication, and asp.net redirectfromlogin method to automatically create a session for any user who already logged in.
My login button has code:
Sub Submit_OnClick(sender as Object, e as EventArgs)
If Authenticate (txtUserName.Text, txtPassword.Text) Then
'Determine this user's status
Dim type as String = Getusertype(txtUserName.Text, txtPassword.Text)
Dim roleList As New ArrayList
roleList.Add("type")
Dim roleListArray As String() = roleList.ToArray(GetType(String))
HttpContext.Current.User = _
New System.Security.Principal.GenericPrincipal(User.Identity, roleListArray)
FormsAuthentication.RedirectFromLoginPage (txtUserName.Text, True)
Else
' Invalid credentials supplied, display message
Response.Write ("Invalid login credentials")
End If
End Sub
Function Authenticate checks if username and pwd are valid.
Function Getusertype checks the database and returns a string with account type.
Then each page will check if the user belongs to a certain group or not:
If User.IsInRole("Administrator") then
' Display sensitive material
End If
PROBLEM :
1)
Since I am using ASP.NET form authentication, I can not set the specific redirect page based on the type of the user account that i get from function Getusertype. *
FormsAuthentication.RedirectFromLoginPage (txtUserName.Text, True)
- this only takes username.
And I would want the code to create a session and redirect to the page I need (based on the account type). This way i will simple use code below to check if the user can see the xxxmain.aspx or not.
If User.IsInRole("xxx") then
' Display sensitive material
End If
*I believe there is a way to select a redirect page for RedirectFromLoginPage method by setting returnURL in the URL string, however I dont really want to post back to a login page and then redirect. Is there any nicer way?
2) I hope I am creating a role correctly:
'Determine this user's status
Dim type as String = getusertype(txtUserName.Text, txtPassword.Text)
Dim roleList As New ArrayList
roleList.Add("Admin")
Dim roleListArray As String() = roleList.ToArray(GetType(String))
HttpContext.Current.User = _
New System.Security.Principal.GenericPrincipal(User.Identity, roleListArray)
Please let me know if I am missing something.
Thanks a lot for all the useful comments,
I appreciate any help or links on the matter.
A.M.