表单身份验证中的Cookie为null [英] Cookie null in Form authentication

查看:117
本文介绍了表单身份验证中的Cookie为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我们使用mvc4开发Web应用程序并使用表单身份验证。每件事情都很好,但我们注意到了很短的几天奇怪的行为。即使由于此用户无法登录而在登录页面中正确完成身份验证,global.asax中的cookie也为null。下面是我的代码。



登录页面。



Userdata =新的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;



}



之后战斗了两天以上。我们发现它依赖于表单身份验证中的用户数据字符串。



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



当我在userdata中的用户信息较少时,一切正常。但是当userdata包含关于用户的大量信息时,则在登录时发生auth,但在globl.asax中cookie变为null。真奇怪。请指导我。



在两个场景中都创建了cookie,我可以在IE11浏览器中看到。



UserData(Less data)包含。 147 charcter - 它工作正常。



UserData(大数据)包含。 1045个字符 - 它不起作用。

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.

推荐答案

这篇关于表单身份验证中的Cookie为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆