Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm doing form authentication with roles to access areas of a website. The authentication is working great, but I'm having trouble with the roles piece. On the page in question (needing to narrow the access), the top lblGrps.text shows I'm in the group I need to be in, while the response.write reads false for the EdIT group:

VB
Try
    Dim authTicket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(Context.Request.Cookies(FormsAuthentication.FormsCookieName).Value)
    Dim myGroups As String = (authTicket.UserData).ToString
    lblGrps.Text = myGroups
Catch ex As Exception
    lblGrps.Text = "empty"
End Try
Response.Write("User name: " & Context.User.Identity.Name & "<br />Authenticated: " & Context.User.Identity.IsAuthenticated.ToString() & "<br />In EdIT role: " & Context.User.IsInRole("EdIT").ToString())

And, the code below denies me access to the page:
VB
Function getMyRole(ByVal myRole As String) As Boolean
    Dim myReturn As Boolean
    If Context.User.IsInRole(myRole) Then
        myReturn = True
    Else
        myReturn = False
    End If
    Return myReturn
End Function

If getMyRole("EdIT") then
'blah, blah,

Any help appreciated.
Posted
Updated 5-Aug-10 6:41am
v2
Comments
Sandeep Mewara 5-Aug-10 12:44pm    
Why are you doing a Response.Write? If you have designed a webpage, then why not using labels and other controls?

The response.write was only temporary to try and trouble shoot the problem. When responding to questions, please be pertinent and kind.
 
Share this answer
 
Is it possible you are calling your getMyRole method before the user has been authenticated? You need to get the auth cookie on each page request. If you get the auth cookie on the first page request but not subsequent page requests then you will have issues.
 
Share this answer
 
RLH88, thanks for the reply. No, I can't get to the page at all unless the user has been authenticated. I have the same set-up, but with different groups for another site, and I'm confused as to why one works great and one doesn't. I can do the following, but it just drives me nuts that it works on one site and not another.

<pre lang="vb">Function getMyRole(ByVal myRole As String) As Boolean
    Dim myReturn As Boolean
    Dim authTicket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(Context.Request.Cookies(FormsAuthentication.FormsCookieName).Value)
    Dim myGroups As String = (authTicket.UserData).ToString
    If InStr(myGroups, myRole) Then
        myReturn = True
    Else
        myReturn = False
    End If
    Return myReturn
End Function

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900