创建和ASP.NET MVC3访问饼干 [英] Creating and accessing Cookies in ASP.NET MVC3

查看:112
本文介绍了创建和ASP.NET MVC3访问饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建在控制器饼干和访问它就像例如任何视图

  User.Identity.Name

我可以使用code的任何地方,因为用户已经登录并且它也被删除,当用户基于默认的code

注销

  FormsAuthentication.SetAuthCookie

另外我想知道如何删除或清除该cookie。


解决方案

  ....
//创建的cookie
VAR饼干=新的HttpCookie(cookieName);cookie.Value =值;
Response.Cookies.Add(饼干);//删除的cookie
VAR饼干=新的HttpCookie(cookieName);
cookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(饼干);//请求的cookie值
VAR VAL = Request.Cookies时[cookieName]值。
....

How can I create cookies in the controller and access it in any view just like for example

User.Identity.Name

I can use that code anywhere since the user has logged in and it's also removed when the user log out based on the default code of

FormsAuthentication.SetAuthCookie

Also I want to know how to delete or clear that cookie.

解决方案

....    
//create cookie
var cookie = new HttpCookie("cookieName");

cookie.Value = "value";
Response.Cookies.Add(cookie);

//remove cookie
var cookie = new HttpCookie("cookieName");
cookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(cookie);

//To Request the cookies value
var val = Request.Cookies["cookieName"].Value;
....

这篇关于创建和ASP.NET MVC3访问饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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