Home > Back-end >  Not sure why FormsAuthentication cookies in an ASP.NET web application are not working for me
Not sure why FormsAuthentication cookies in an ASP.NET web application are not working for me

Time:07-31

I am having issues getting my ASP.NET web application to either read or find (not sure which) the FormsAuthentication cookie I create when a new user registers. Here's the code for creating the cookie:

            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user.UserName, DateTime.Now, DateTime.Now.AddDays(30),
        true, null, FormsAuthentication.FormsCookiePath);
            string encTicket = FormsAuthentication.Encrypt(ticket);
            Response.Cookies.Add ( new HttpCookie ( FormsAuthentication.FormsCookieName, encTicket ) );
            if ( !Roles.RoleExists ( "Members" ) )
                Roles.CreateRole ( "Members" );
            if ( !Roles.IsUserInRole ( user.UserName, "Members" ) )
                Roles.AddUserToRole ( user.UserName, "Members" );

And here are the settings in the web.config file for forms authentication:

      <authentication mode="Forms" >
        <forms loginUrl="signin.aspx" name="P411" path="/" cookieless="AutoDetect" slidingExpiration="true" />
      </authentication>

In one of my master pages I am checking the HttpContext.Current.User.Identity.IsAuthenticated property to either display a messag e in the header to authenticated users or a different message to unregistered/unauthenticated ones. It always returns "false" for the IsAuthenticated property, even though the registration code functions correctly. I don't know if I am setting the properties wrong in web.config, creating the cookie incorrectly, or if I have to do something else to read the cookie's properties correctly. I could use some help with this. Thanks!

CodePudding user response:

Try changing DateTime.Now in FormsAuthenticationTicket to DateTime.UTCNow

  • Related