System.Web.Security.FormsAuthentication.Encrypt返回null [英] System.Web.Security.FormsAuthentication.Encrypt returns null

查看:116
本文介绍了System.Web.Security.FormsAuthentication.Encrypt返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加密用户数据的一些创建自己的自定义使用窗体身份验证的IPrincipal和IIdentity的对象 - 我已经序列化的对象重新presenting我登录的用户为JSON和创建我的FormsAuthentication票像这样:

I'm trying to encrypt some userData to create my own custom IPrincipal and IIdentity objects using Forms authentication - I've serialized an object representing my logged in user to Json and created my FormsAuthentication ticket like so:

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字符串的长度?

However, the string encrypted_ticket is always null. Is there a limit on the length of the user_item string?

谢谢
穆斯塔法

Thanks Mustafa

推荐答案

是的,典型的cookie限制为4K〜

Yes, the typical cookie limit is ~4k.

添加加密,并且您下降到< 2K

Add encryption and you are down to <2k.

您code是正确的..考虑:

Your code is correct.. consider:

string user_item = "fsddfdfssdfsfdasdfsf";

System.Web.Security.FormsAuthenticationTicket ticket =
    new System.Web.Security.FormsAuthenticationTicket(1,
     " sdfasdf asdflasdfasd ",
    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);

收益率:


95ED981CFDF6AE506650E2AD01D2B52666AFC4895F0E19F14D82628C3F263EF1DA77F73BFD0284BEDCF815FBFB9AF00AF8875C61D01E27BF53C229F19C7CDE54FBC10AC478FAF02237DDC545AEF32BBA8EED88DBB09D671CD87E685E9FE05908CAE02EB05880DC1D230D67AEB0D7B0F258435D906EBD7F505DCCD738D94532E96363B13DA92060720476619672FEC670

虽然这是我的经验,而不是置空臃肿的cookie将被截断,您的问题可能是JSON包含的字符,这将使你的cookie畸形,从而打破了。

While it is my experience that bloated cookies are truncated as opposed to nulled, your issue is probably that JSON contains characters that will make your cookie malformed, thus breaking it.

请确保您的JSON是一个合理的规模,然后尝试

Make sure your json is a reasonable size, then try

string user_item = Server.UrlEncode(GetJsonOfLoggedinUser());

请确保你衡量你的饼干,不要试图推它,它会在潜移默化和恶性咬的方式,当你想在家看失落,喝龙舌兰酒。没有什么好玩的。

Make sure you measure your cookies and don't try to push it, it will bite in subtle and vicious ways when you want to be home watching Lost and drinking tequila. no fun.

这篇关于System.Web.Security.FormsAuthentication.Encrypt返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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