Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Accountcontrolle中设置cookie_C#_Asp.net Mvc_Vb.net - Fatal编程技术网

C# 在Accountcontrolle中设置cookie

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

Response.Cookies.Addnew HttpCookieFormsAuthentication.FormsCookieName,encTicket; 我使用这段代码,但它返回的对象引用未设置为对象的实例。
它有什么问题

您应该在构造函数之外创建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);
如果有的话,这至少可以让您找出导致空引用异常的原因,如果不能完全解决问题的话。