Cannot retrieve UserData in Forms Authentication

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default Cannot retrieve UserData in Forms Authentication

    I am using FormsAuthentication for my application, and in
    the UserData property of the FormsAuthenticationTicket
    I'm storing the roles that the user is a member of, to
    retrieve in global.asax and create a GenericPrincipal
    object. Problem is, after the user logs in, I can get
    the ticket just fine from the cookie, and all the data is
    there -- except for the UserData property. It's empty.

    I set the UserData as follows:

    Dim authTicket As New FormsAuthenticationTicket(1,
    txtUserID.Text, DateTime.Now, DateTime.Now.AddHours(1),
    False, GetRoles(txtUserID.Text))
    Dim encryptedTicket As String =
    FormsAuthentication.Encrypt(authTicket)
    Dim authCookie As New HttpCookie
    (FormsAuthentication.FormsCookieName, encryptedTicket)
    Response.Cookies.Add(authCookie)

    Then I get the same cookie back in global.asax:

    Dim cookieName As String =
    FormsAuthentication.FormsCookieName
    Dim authCookie As HttpCookie = Context.Request.Cookies
    (cookieName)

    If authCookie Is Nothing Then Exit Sub

    Dim authTicket As FormsAuthenticationTicket =
    FormsAuthentication.Decrypt(authCookie.Value)

    If authTicket Is Nothing Then Exit Sub

    Dim roles() As String = Split
    (authTicket.UserData, "|") ' Roles are in the
    format "Role1|Role2|...|RoleN"

    Dim id As New FormsIdentity(authTicket)

    Dim principal As New GenericPrincipal(id, roles)

    Context.User = principal

    And the user isn't in the given roles. I went back and
    put:

    Response.Write("'" & authTicket.UserData & "'")

    in the global.asax file, and I get an empty string.
    Anyone have an idea?
    John Kievlan Guest

  2. Similar Questions and Discussions

    1. Accessing htm files without authentication (forms authentication)
      I have application with forms authentication. All works fine. When user opens .aspx file gets login form, login and then get the .aspx page. But...
    2. ASP.Net Forms authentication with basic authentication popup
      Relatively new to ASP.Net but have a strange problem. My site uses forms authentication for a large administration section however after the user...
    3. Forms authentication then redirection to a secure web with NT authentication?
      Hi, I want to allow access to particular secured intranet web sites. These intranet are stored in sharepoint (2003 version) Actually I've...
    4. Authentication ticket, cookieless, forms authentication?
      Hi. I want to use Forms Authentication, cookieless. The issue is setting the Authentication Ticket without using cookies (!) That is, the...
    5. Forms authentication with Windows authentication
      Hi, I have an ASP.NET web site that uses IIS Basic Authentication and accesses an OLAP Server at various stages. The OLAP Server authentication...
  3. #2

    Default Cannot retrieve UserData in Forms Authentication

    Your code matches what I use. And I just re-ran my code,
    and am getting user data back from the cookie.

    I assume your setting the ticket in a login page. And
    have added the necessary web.config entries. I haven't
    tried my code against a browser that doesn't accept
    cookies.

    Sorry that the only assitance is to note that your code
    appears correct. I'm only guessing that the problem lies
    in configuration.
    >-----Original Message-----
    >I am using FormsAuthentication for my application, and in
    >the UserData property of the FormsAuthenticationTicket
    >I'm storing the roles that the user is a member of, to
    >retrieve in global.asax and create a GenericPrincipal
    >object. Problem is, after the user logs in, I can get
    >the ticket just fine from the cookie, and all the data is
    >there -- except for the UserData property. It's empty.
    >
    >I set the UserData as follows:
    >
    >Dim authTicket As New FormsAuthenticationTicket(1,
    >txtUserID.Text, DateTime.Now, DateTime.Now.AddHours(1),
    >False, GetRoles(txtUserID.Text))
    >Dim encryptedTicket As String =
    >FormsAuthentication.Encrypt(authTicket)
    >Dim authCookie As New HttpCookie
    >(FormsAuthentication.FormsCookieName, encryptedTicket)
    >Response.Cookies.Add(authCookie)
    >
    >Then I get the same cookie back in global.asax:
    >
    >Dim cookieName As String =
    >FormsAuthentication.FormsCookieName
    >Dim authCookie As HttpCookie = Context.Request.Cookies
    >(cookieName)
    >
    >If authCookie Is Nothing Then Exit Sub
    >
    >Dim authTicket As FormsAuthenticationTicket =
    >FormsAuthentication.Decrypt(authCookie.Value)
    >
    >If authTicket Is Nothing Then Exit Sub
    >
    >Dim roles() As String = Split
    >(authTicket.UserData, "|") ' Roles are in the
    >format "Role1|Role2|...|RoleN"
    >
    >Dim id As New FormsIdentity(authTicket)
    >
    >Dim principal As New GenericPrincipal(id, roles)
    >
    >Context.User = principal
    >
    >And the user isn't in the given roles. I went back and
    >put:
    >
    >Response.Write("'" & authTicket.UserData & "'")
    >
    >in the global.asax file, and I get an empty string.
    >Anyone have an idea?
    >.
    >
    tom hamilton Guest

  4. #3

    Default Re: Cannot retrieve UserData in Forms Authentication

    Did you solve your problem with user data is empty???
    I am facing the same problem, I really cannot get any solution to solve this problem
    can you tell me how your solve it?
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139