我正在尝试加密一些userData以使用Forms身份验证创建自己的自定义IPrincipal和IIdentity对象 – 我已经将表示我登录的用户的对象序列化为Json,并创建了我的FormsAuthentication票证,如下所示:
string user_item = GetJsonOfLoggedinUser();/*get JSON representation of my logged in user*/ System.Web.Security.FormsAuthenticationTicket ticket = new System.Web.Security.FormsAuthenticationTicket(1,WAM.Utilities.SessionHelper.LoggedInEmployee.F_NAME + " " + WAM.Utilities.SessionHelper.LoggedInEmployee.L_NAME,DateTime.Now,DateTime.Now.AddMinutes(30),false,user_item); string encrypted_ticket = System.Web.Security.FormsAuthentication.Encrypt(ticket); HttpCookie auth_cookie = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName,encrypted_ticket); Response.Cookies.Add(auth_cookie);
但是,“encrypted_ticket”字符串始终为空. user_item字符串的长度有限制吗?
谢谢
穆斯塔法
解决方法
是的,典型的cookie限制是〜4k.
添加加密功能,您将达到< 2k. 你的代码是正确的..考虑:
string user_item = "fsddfdfssdfsfdasdfsf"; System.Web.Security.FormsAuthenticationTicket ticket = new System.Web.Security.FormsAuthenticationTicket(1," sdfasdf asdflasdfasd ",user_item); string encrypted_ticket = System.Web.Security.FormsAuthentication.Encrypt(ticket); HttpCookie auth_cookie = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName,encrypted_ticket);
产量:
95ED981CFDF6AE506650E2AD01D2B52666AFC4895F0E19F14D82628C3F263EF1DA77F73BFD0284BEDCF815FBFB9AF00AF8875C61D01E27BF53C229F19C7CDE54FBC10AC478FAF02237DDC545AEF32BBA8EED88DBB09D671CD87E685E9FE05908CAE02EB05880DC1D230D67AEB0D7B0F258435D906EBD7F505DCCD738D94532E96363B13DA92060720476619672FEC670
虽然我的经验是膨胀的Cookie被截断而不是null,但是您的问题可能是JSON包含会使您的cookie畸形的字符,从而破坏它.
确保你的json是一个合理的大小,然后尝试
string user_item = Server.UrlEncode(GetJsonOfLoggedinUser());
确保你测量你的饼干,不要试图推动它,当你想要回家看迷失和喝龙舌兰酒时,它会以微妙和恶毒的方式咬伤.不好玩.