Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problems with FormsAuthenticationTicket in role-base security

Problems with FormsAuthenticationTicket in role-base security

Scheduled Pinned Locked Moved ASP.NET
questionsecuritydebugging
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Flores
    wrote on last edited by
    #1

    I am having problems with the FormsAuthenticationTicket keeping the UserData that I store in it when creating the ticket. The UserData (i.e. my role information) is getting lost. My login code is the following (simpiflied)

    'returns "SA" for this demo
    Dim roles As String = GetRoles()
    Dim authTicket As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, Me.txtLogin.Text, DateTime.Now, DateTime.Now.AddMinutes(60), False, roles)
    Dim encryptedTicket As String = FormsAuthentication.Encrypt(authTicket)
    Dim authCookie As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
    Response.Cookies.Add(authCookie)
    FormsAuthentication.RedirectFromLoginPage(Me.txtLogin.Text, False)

    Now when a page gets authenticated the code, Application_AuthenticateRequest, in the global.asax the following happens (again simplified)

    Dim cookieName As String = FormsAuthentication.FormsCookieName
    Dim authCookie As HttpCookie = Context.Request.Cookies(cookieName)
    Dim authTicket As FormsAuthenticationTicket = Nothing
    authTicket = FormsAuthentication.Decrypt(authCookie.Value)

    'this should be "SA" but UserData is ""
    Dim roles As String() = authTicket.UserData.Split("|".ToCharArray)

    Dim id As FormsIdentity = New FormsIdentity(authTicket)
    Dim principal As CustomPrincipal = New CustomPrincipal(id, roles)
    Context.User = principal

    If I stop the execution in the debugger and modify the value of roles to be "SA" then everything else works fine. The big question is, Why is UserData empty? Thanks in advance.

    D 1 Reply Last reply
    0
    • D David Flores

      I am having problems with the FormsAuthenticationTicket keeping the UserData that I store in it when creating the ticket. The UserData (i.e. my role information) is getting lost. My login code is the following (simpiflied)

      'returns "SA" for this demo
      Dim roles As String = GetRoles()
      Dim authTicket As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, Me.txtLogin.Text, DateTime.Now, DateTime.Now.AddMinutes(60), False, roles)
      Dim encryptedTicket As String = FormsAuthentication.Encrypt(authTicket)
      Dim authCookie As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
      Response.Cookies.Add(authCookie)
      FormsAuthentication.RedirectFromLoginPage(Me.txtLogin.Text, False)

      Now when a page gets authenticated the code, Application_AuthenticateRequest, in the global.asax the following happens (again simplified)

      Dim cookieName As String = FormsAuthentication.FormsCookieName
      Dim authCookie As HttpCookie = Context.Request.Cookies(cookieName)
      Dim authTicket As FormsAuthenticationTicket = Nothing
      authTicket = FormsAuthentication.Decrypt(authCookie.Value)

      'this should be "SA" but UserData is ""
      Dim roles As String() = authTicket.UserData.Split("|".ToCharArray)

      Dim id As FormsIdentity = New FormsIdentity(authTicket)
      Dim principal As CustomPrincipal = New CustomPrincipal(id, roles)
      Context.User = principal

      If I stop the execution in the debugger and modify the value of roles to be "SA" then everything else works fine. The big question is, Why is UserData empty? Thanks in advance.

      D Offline
      D Offline
      David Flores
      wrote on last edited by
      #2

      The problem was using FormsAuthentication.RedirectFromLoginPage, it writes its own cookie which over writes yours!!!:mad: So I change it to a Response.Redirect and everything works fine. :-D
      However...It took me two days to figure that out because it is not in the documentation. ugh!
      Later

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups