ASP.Net MVC 5中的Cookie [英] Cookies in ASP.Net MVC 5

查看:84
本文介绍了ASP.Net MVC 5中的Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中的用户可以通过外部身份提供商(例如AAD,Google,WS-Feedated Authentication等)进行注册或登录.现在,我想在用户计算机上创建Cookie,直到用户注销后才能登录.请给我一些想法,并指导我如何克服它.预先感谢.

I am developing an application in which users are SignUp or SignIn by External Identity Providers like AAD, Google, WS-Federated Authentication etc. Now I want to create cookies on a user machine to logged in until user SignOut. Give me some thought and guide me how I can overcome it. thanks in advance.

推荐答案

使用 Request.Cookies Response.Cookies 处理您的情况.一旦从第三方授权回来的用户创建cookie并将其存储在浏览器中,并且一旦用户注销清除cookie.

Use Request.Cookies and Response.Cookies to handle your situation. once user coming back from third party authorization create cookie and store it in browser and once user Logout clear the cookie.

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

要删除Cookie,请使用以下代码

For removing cookie use following code

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

这篇关于ASP.Net MVC 5中的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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