假设我有一个域名网站:www.example.com
如果我使用路径' / '设置cookie,则可以通过域中的所有页面访问cookie,例如:
如果我们将cookie设置为路径' / subfolder1 ',该cookie是否可用于该文件夹下的任何页面或子文件夹?例如:
所以,如果没有,我猜,我别无选择,只能对这些cookie使用路径'/',对吗?
我在Asp.Net MVC4中有Web应用程序,我想使用cookie进行用户登录和注销.所以我的行动如下:
登录操作
[HttpPost]
public ActionResult Login(string username, string pass)
{
if (ModelState.IsValid)
{
var newUser = _userRepository.GetUserByNameAndPassword(username, pass);
if (newUser != null)
{
var json = JsonConvert.SerializeObject(newUser);
var userCookie = new HttpCookie("user", json);
userCookie.Expires.AddDays(365);
HttpContext.Response.Cookies.Add(userCookie);
return RedirectToActionPermanent("Index");
}
}
return View("UserLog");
}
Run Code Online (Sandbox Code Playgroud)
LogOut Action
public ActionResult UserOut()
{
if (Request.Cookies["user"] != null)
{
var user = new HttpCookie("user")
{
Expires = DateTime.Now.AddDays(-1),
Value = null
};
Response.Cookies.Add(user);
}
return RedirectToActionPermanent("UserLog");
}
Run Code Online (Sandbox Code Playgroud)
我在_Loyout中使用此cookie如下:
@using EShop.Core
@using …Run Code Online (Sandbox Code Playgroud) 默认情况下,使用C#代码添加cookie的过期时间是多少?
HttpCookie myCookie= new HttpCookie("myCookie");
myCookie.Value = txtCookie.Text;
// Add the cookie.
Response.Cookies.Add(myCookie);
Run Code Online (Sandbox Code Playgroud) 我只是想澄清一下.
我知道,如果我在之前的请求中设置了cookie,它将显示在我的Request.Cookies收藏中.
我想更新现有的Cookie.
我的Request.Cookies收藏中的cookie是否已复制到我的Response.Cookies收藏中?我是否需要使用相同的密钥添加新的cookie Response.Cookies.Add(),或者我是否需要使用Response.Cookies.Set()?
asp.net httpresponse httprequest httpcookie httpcookiecollection
如果我setcookie()使用相同的cookie名称拨打两次,我会创建两个cookie.
你如何更新现有的cookie?
今天早上我偶然看到了以下片段代码,我感到非常惊讶,因为它工作得非常好.
请不要看它的逻辑,我只是好奇为什么HttpCookieCollection(在这种情况下是Request.Cookies)在foreach循环中返回一个字符串(cookie名称)而不是HttpCookie对象.这是一个一致性问题,因为我们通常通过索引/名称在此集合中获取HttpCookie对象吗?
谢谢,
foreach (string cookieKey in System.Web.HttpContext.Current.Request.Cookies)
{
HttpCookie tmpCookie = System.Web.HttpContext.Current.Request.Cookies[cookieKey];
if (tmpCookie != null && tmpCookie["RecentlyVisited"] != null)
{
cookie.Add(tmpCookie);
}
}
Run Code Online (Sandbox Code Playgroud) 我使用以下语法来设置cookie:
Set-Cookie:Cookie-name=value; path=/; Max-Age=1296000; HttpOnly
Run Code Online (Sandbox Code Playgroud)
在谷歌Chrome控制台中,它显示 该cookie的无效日期.
语法有什么问题?
根据 http://en.wikipedia.org/wiki/HTTP_cookie#Expires_and_Max-Age http://tools.ietf.org/html/rfc6265#section-5.2.2我可以使用Max-Age来指定相对到期时间.
分号;,Cookie:字符串或其他字符串?
我尝试在我的应用程序中实现一个基本的cookie帮助器.主要是我每次检查基本控制器是否设置了cookie.如果是cookie
public class MyCookie
{
public static string CookieName {get;set;}
public virtual User User { get; set; }
public virtual Application App { get; set; }
public MyCookie(Application app)
{
CookieName = "MyCookie" + app;
App = app;
}
public void SetCookie(User user)
{
HttpCookie myCookie = HttpContext.Current.Request.Cookies[CookieName] ?? new HttpCookie(CookieName);
myCookie.Values["UserId"] = user.UserId.ToString();
myCookie.Values["LastVisit"] = DateTime.Now.ToString();
myCookie.Expires = DateTime.Now.AddDays(365);
HttpContext.Current.Response.Cookies.Add(myCookie);
}
public HttpCookie GetCookie()
{
HttpCookie myCookie = HttpContext.Current.Request.Cookies[CookieName];
if(myCookie != null)
{
int userId = Convert.ToInt32(myCookie.Values["UserId"]);
User user …Run Code Online (Sandbox Code Playgroud) 所以我很困惑,因为msdn和其他教程告诉我使用HttpCookies通过Response.Cookies.Add(cookie)添加cookie.但这就是问题所在.Response.Cookies.Add只接受Cookies而不接受HttpCookies,我收到此错误:
无法从'System.Net.CookieContainer'转换为'System.Net.Cookie'
另外,Response.Cookies.Add(cookie)和Request.CookieContainer.Add(cookie)之间有什么区别?
感谢您的帮助,我正在尝试使用C#自学.
// Cookie
Cookie MyCookie = new Cookie();
MyCookie.Name = "sid";
MyCookie.Value = SID;
MyCookie.HttpOnly = true;
MyCookie.Domain = ".domain.com";
// HttpCookie
HttpCookie MyCookie = new HttpCookie("sid");
MyCookie.Value = SID;
MyCookie.HttpOnly = true;
MyCookie.Domain = ".domain.com";
Response.Cookies.Add(MyCookie);
Run Code Online (Sandbox Code Playgroud) httpcookie ×10
cookies ×6
asp.net ×4
c# ×4
http-headers ×2
action ×1
asp.net-mvc ×1
cakephp ×1
cookiejar ×1
http ×1
httprequest ×1
httpresponse ×1
php ×1
rfc ×1