C# 在Accountcontrolle中设置cookie
Response.Cookies.Addnew HttpCookieFormsAuthentication.FormsCookieName,encTicket; 我使用这段代码,但它返回的对象引用未设置为对象的实例。C# 在Accountcontrolle中设置cookie,c#,asp.net-mvc,vb.net,C#,Asp.net Mvc,Vb.net,Response.Cookies.Addnew HttpCookieFormsAuthentication.FormsCookieName,encTicket; 我使用这段代码,但它返回的对象引用未设置为对象的实例。 它有什么问题您应该在构造函数之外创建cookie,这样您至少可以了解它抛出异常的原因 通常,对于类似的情况,我会执行以下操作: // create the auth cookie with the domain you've specified var cookie = new H
它有什么问题您应该在构造函数之外创建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);
如果有的话,这至少可以让您找出导致空引用异常的原因,如果不能完全解决问题的话。