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. UserData Problem

UserData Problem

Scheduled Pinned Locked Moved ASP.NET
helpdatabasesecuritycryptographyregex
3 Posts 2 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.
  • S Offline
    S Offline
    Sean89
    wrote on last edited by
    #1

    I have a login page in a website where I am authenticating the user via an access database. When I am creating the cookie for the authenticated user, and I put the users roles into the UserData property of the FormsAuthentication ticket something weird happens. I noticed that the users roles weren't working so I did some debugging. I noticed that when the application calls Application_AuthenticateRequest in the Global.asax file, the UserData property in the users ticket is empty. However, all of the other data is there :confused: Here is where I am authenticating the user:

        // verify that the passwords match
        if (userInfo\[0\] == entrdPwd)
        {
            // Password is good. User is authenticated.
    
            // Create a new ticket used for authentication
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName,DateTime.Now, DateTime.Now.AddMinutes(30), 
                                               true, userInfo\[1\], FormsAuthentication.FormsCookiePath);
    
            // Encrypt the cookie using the machine key for secure transport
            string hash = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); 
    
            // Set the cookie's expiration time to the tickets expiration time
            if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
    
            // Add the cookie to the list for outgoing response
            Response.Cookies.Add(cookie);
    
            return true;
        }
    

    Everything is working fine there as far as i can tell. This is where the problem is:

    void Application\_AuthenticateRequest(object sender, EventArgs e)
    {
        if (User != null)
        {
            if (User.Identity.IsAuthenticated)
            {
                FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                FormsAuthenticationTicket ticket = id.Ticket;
    
                // Get the stored user-data, in this case, our roles
                string\[\] role = new string\[1\];
                role\[0\] = ticket.UserData;    // UserData is empty ("") ???
                HttpContext.Current.User = new GenericPrincipal(id, role);
            }  
        }
    }
    

    Thanks for any help ;P


    M 2 Replies Last reply
    0
    • S Sean89

      I have a login page in a website where I am authenticating the user via an access database. When I am creating the cookie for the authenticated user, and I put the users roles into the UserData property of the FormsAuthentication ticket something weird happens. I noticed that the users roles weren't working so I did some debugging. I noticed that when the application calls Application_AuthenticateRequest in the Global.asax file, the UserData property in the users ticket is empty. However, all of the other data is there :confused: Here is where I am authenticating the user:

          // verify that the passwords match
          if (userInfo\[0\] == entrdPwd)
          {
              // Password is good. User is authenticated.
      
              // Create a new ticket used for authentication
              FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName,DateTime.Now, DateTime.Now.AddMinutes(30), 
                                                 true, userInfo\[1\], FormsAuthentication.FormsCookiePath);
      
              // Encrypt the cookie using the machine key for secure transport
              string hash = FormsAuthentication.Encrypt(ticket);
              HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); 
      
              // Set the cookie's expiration time to the tickets expiration time
              if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
      
              // Add the cookie to the list for outgoing response
              Response.Cookies.Add(cookie);
      
              return true;
          }
      

      Everything is working fine there as far as i can tell. This is where the problem is:

      void Application\_AuthenticateRequest(object sender, EventArgs e)
      {
          if (User != null)
          {
              if (User.Identity.IsAuthenticated)
              {
                  FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                  FormsAuthenticationTicket ticket = id.Ticket;
      
                  // Get the stored user-data, in this case, our roles
                  string\[\] role = new string\[1\];
                  role\[0\] = ticket.UserData;    // UserData is empty ("") ???
                  HttpContext.Current.User = new GenericPrincipal(id, role);
              }  
          }
      }
      

      Thanks for any help ;P


      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      What do you do after the authentication code (the code in the first paragraph) is executed? What do the authentication settings look like? Also, do you post the question below Heath's article (he might give you a hint)?

      1 Reply Last reply
      0
      • S Sean89

        I have a login page in a website where I am authenticating the user via an access database. When I am creating the cookie for the authenticated user, and I put the users roles into the UserData property of the FormsAuthentication ticket something weird happens. I noticed that the users roles weren't working so I did some debugging. I noticed that when the application calls Application_AuthenticateRequest in the Global.asax file, the UserData property in the users ticket is empty. However, all of the other data is there :confused: Here is where I am authenticating the user:

            // verify that the passwords match
            if (userInfo\[0\] == entrdPwd)
            {
                // Password is good. User is authenticated.
        
                // Create a new ticket used for authentication
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName,DateTime.Now, DateTime.Now.AddMinutes(30), 
                                                   true, userInfo\[1\], FormsAuthentication.FormsCookiePath);
        
                // Encrypt the cookie using the machine key for secure transport
                string hash = FormsAuthentication.Encrypt(ticket);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); 
        
                // Set the cookie's expiration time to the tickets expiration time
                if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
        
                // Add the cookie to the list for outgoing response
                Response.Cookies.Add(cookie);
        
                return true;
            }
        

        Everything is working fine there as far as i can tell. This is where the problem is:

        void Application\_AuthenticateRequest(object sender, EventArgs e)
        {
            if (User != null)
            {
                if (User.Identity.IsAuthenticated)
                {
                    FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                    FormsAuthenticationTicket ticket = id.Ticket;
        
                    // Get the stored user-data, in this case, our roles
                    string\[\] role = new string\[1\];
                    role\[0\] = ticket.UserData;    // UserData is empty ("") ???
                    HttpContext.Current.User = new GenericPrincipal(id, role);
                }  
            }
        }
        

        Thanks for any help ;P


        M Offline
        M Offline
        minhpc_bk
        wrote on last edited by
        #3

        Hi Sean, I only can read half of your reply in the email sent to me, the rest of it simply said "...(continued)", I guess you might have clicked the Email link instead of the Reply. Fortunately, the first half of the email may help me figure out the cause (hopefully). Well, after the login control authenticates the user, if the user is authenticated, the control will add the authentication cookie with the same name which you use it your own method. This happens after the Authenticate event, so it may override your own cookie. So your authenticate code should validate the user credentials only, and you can create an event handler for the LoggedIn event to add your authentication cookie. In addition, you may also want to check out the Roles Management in the ASP.NET 2.0 instead of managing on your own. http://msdn2.microsoft.com/en-us/library/53s18z5c.aspx[^] http://msdn.microsoft.com/library/en-us/dnpag2/html/PAGHT000013.asp[^]

        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