User.isInrole problem

Last post 02-12-2008 3:51 AM by GillouX. 5 replies.

Sort Posts:

  • User.isInrole problem

    02-11-2008, 5:37 AM
    • Contributor
      2,050 point Contributor
    • GillouX
    • Member since 06-03-2007, 11:28 AM
    • Luxemburg
    • Posts 561

    Hello

    I have a problem using the user.isinrole ; it returns false any time.

    It's really strange because when I set my genericprincipal :
     

    Dim objIdentity As GenericIdentity = New GenericIdentity(_username)
            Dim objPrincipal As GenericPrincipal = New GenericPrincipal(objIdentity, _roles)
            Thread.CurrentPrincipal = objPrincipal
            HttpContext.Current.User = objPrincipal

    on debug if I make user.isinrole("Admin"), I get true

    but on the on load of another page, I get false however I can get still my User.identiy.name correctly

     Why I'm losing this property ?

    Thx

    GillouX 


     

     

     

  • Re: User.isInrole problem

    02-11-2008, 6:57 AM
    • Member
      2 point Member
    • MarcinM
    • Member since 02-11-2008, 8:34 AM
    • Posts 3

    Try checking role with domain prefix, ex: User.IsInRole(@"domain\user").

    If that fails try this code:

    string[] members = Roles.GetRolesForUser();
    foreach (string role in members)
    {
    Response.Write("\n" + role + "/<br />");
    }

      ... and dont forget enable roleManager in web.config: 

    <system.web>
    <roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider"/>
    </systen.web>
     

     

  • Re: User.isInrole problem

    02-11-2008, 7:06 AM
    • Contributor
      2,117 point Contributor
    • dharnendra
    • Member since 12-26-2007, 5:04 AM
    • Posts 395

    Hi refer the below link 

    http://www.velocityreviews.com/forums/t221583-contextuser-problem.html

    Shah Dharnendra G
    Sr.Analyst Programmer,
    GTL-Ahmedabad
  • Re: User.isInrole problem

    02-11-2008, 7:47 AM
    • Contributor
      2,050 point Contributor
    • GillouX
    • Member since 06-03-2007, 11:28 AM
    • Luxemburg
    • Posts 561

    I checked the last link and it's strange what it says there

     

     context is valid for a single request (page render). when you redirect to a
    new page, that page gets a new context. you need to store your
    authentication info somewhere that the client will send to you on each
    request. (say a cookie or url munging)

    why this would keep the name and not the userinrole property so ?

  • Re: User.isInrole problem

    02-11-2008, 3:07 PM
    Answer
    • Contributor
      2,050 point Contributor
    • GillouX
    • Member since 06-03-2007, 11:28 AM
    • Luxemburg
    • Posts 561

    This code made the trick

     

     Dim tkt As FormsAuthenticationTicket
                Dim cookiestr As String
                Dim ck As HttpCookie

                tkt = New FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now(), _
          DateTime.Now.AddMinutes(30), False, "your custom data")
                cookiestr = FormsAuthentication.Encrypt(tkt)
                ck = New HttpCookie(FormsAuthentication.FormsCookieName(), cookiestr)
                          
                ck.Path = FormsAuthentication.FormsCookiePath()
                Response.Cookies.Add(ck)

     Thx anyway

     

  • Re: User.isInrole problem

    02-12-2008, 3:51 AM
    • Contributor
      2,050 point Contributor
    • GillouX
    • Member since 06-03-2007, 11:28 AM
    • Luxemburg
    • Posts 561

    My mistake,

    this code is not working, I figured it out now that I had already changed sg else.

     

    This

     

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)

    If Request.IsAuthenticated() Then

    'create an array of roles for the current user

    'these would most likely be dynamically read

    ' from the data store for each user.

    Dim arrRoles() As String = {"Admin"}

     

    'Add our Principal to the current context

    'Thread.CurrentPrincipal = New GenericPrincipal(Context.User.Identity, arrRoles)

    HttpContext.Current.User = New System.Security.Principal.GenericPrincipal(Context.User.Identity, arrRoles)

     

    End If

    End Sub

     

    in the Global.asax made it working.

     

    Can sy ask me why it has to be in the global.asax ?

    I tried this use this swelse and I still get false

Page 1 of 1 (6 items)