Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi we developing web application using mvc4 and used form authentication. Every thing works fine but we noticed strange behavior for quite few days. the cookie in global.asax is null even though authentication done properly in login page due to this user not able to login. Below is my code.

Login Page.

Userdata = new JavaScriptSerializer().Serialize(userData);
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
UserDetails.UserID,
DateTime.Now,
DateTime.Now.AddMinutes(90),
false,
Userdata);

string encTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
Response.Cookies.Add(faCookie);

Global.asax

protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{

FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

var s = new System.Web.Script.Serialization.JavaScriptSerializer();
UserInfoAuth obj = s.Deserialize<userinfoauth>(authTicket.UserData);

UserInformation CurrentUser = new UserInformation(obj.UserId);
CurrentUser.FirstName = obj.FirstName;
CurrentUser.LastName = obj.LastName;
CurrentUser.ClientIdentity = Convert.ToInt32(obj.ClientIdentity);
CurrentUser.UserIdentity = Convert.ToInt32(obj.UserIdentity);

HttpContext.Current.User = CurrentUser;

}

After fighting for more then two days. We found it depend upon User data string in form authentication.

FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1, UserDetails.UserID, DateTime.Now, DateTime.Now.AddMinutes(90), false, Userdata);

When i had less user info in userdata all works fine. But when userdata contain large info about user then auth happening in login but in globl.asax cookie becoming null. Its strange. Please guide me.

In both scenario cookie is created i can see in my IE11 browser.

The UserData (Less data) contain. 147 charcter - it working fine.

The UserData (Large data) contain. 1045 character - it not working.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900