2016-06-14 94 views
11

我正在開發一個應用程序,用戶通過外部身份提供程序(如AAD,Google,WS-Federated Authentication等)進行SignUp或SignIn登錄。現在我想在用戶計算機上創建cookie以登錄直到用戶SignOut。給我一些想法,並指導我如何克服它。提前致謝。ASP.Net中的Cookie MVC 5

回答

20

使用Request.CookiesResponse.Cookies來處理您的情況。一旦用戶從第三方授權回來,創建cookie並將其存儲在瀏覽器中,並且一旦用戶註銷清除cookie。

string cookievalue ; 
if (Request.Cookies["cookie"] != null) 
{ 
    cookievalue = Request.Cookies["cookie"].Value.ToString(); 
} 
else 
{ 
    Response.Cookies["cookie"].Value = "cookie value"; 
} 

爲了去除cookie的使用下面的代碼

if (Request.Cookies["cookie"] != null) 
{ 
    Response.Cookies["cookie"].Expires = DateTime.Now.AddDays(-1); 
} 
+0

那麼如何使用此代碼? ControllerContext.HttpContext.Request.Cookies.Add(new HttpCookie(「aa」){Expires = new DateTime()。AddDays(5),HttpOnly = true,Value =「val」}); –