Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName,encTicket)); 我使用此代码,但它返回Object引用未设置为对象的实例。 它有什么问题
答案 0 :(得分:0)
你应该在构造函数之外创建cookie,这样你至少可以看出它抛出异常的原因。
通常对于这样的事情,我会做以下事情:
// create the auth cookie with the domain you've specified
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName)
{
Domain = FormsAuthentication.CookieDomain;
};
// create the auth ticket and encrypt it
var authTicket = new FormsAuthenticationTicket(1, "USERS_EMAIL_OR_USERNAME", DateTime.Now, DateTime.Now.AddHours(24), true, "ANY_USER_INFO_THAT_SHOULD_GO_INTO_THE_COOKIE");
var encryptedTicket = FormsAuthentication.Encrypt(authTicket);
// set the cookie value to the encrypted ticket
cookie.Value = encryptedTicket;
// now, add it to the response, but remove the old one in case it's still there
Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
Response.Cookies.Add(cookie);
如果有的话,至少可以让你找出造成你的空引用异常的原因,如果不能完全修复这个问题。