ASP.Net MVC Cookies不会持续存在
发布时间:2020-05-22 12:16:05 所属栏目:asp.Net 来源:互联网
导读:基本上我是在用户登录后设置一个cookie,以便在他们下次登录时保留他们的用户名.这是我设置cookie的代码.当我在设置cookie后立即查看Firefox中的站点cookie时,它会显示sessionID cookie,但不会显示我刚设置的cookie.当我检查Fiddler中的标题时,我没有看到它设
|
基本上我是在用户登录后设置一个cookie,以便在他们下次登录时保留他们的用户名.这是我设置cookie的代码.当我在设置cookie后立即查看Firefox中的站点cookie时,它会显示sessionID cookie,但不会显示我刚设置的cookie.当我检查Fiddler中的标题时,我没有看到它设置cookie,只有我的sessionID cookie. HttpCookie hc = new HttpCookie("username",model.UserName);
hc.Expires = DateTime.Now.AddYears(1);
System.Web.HttpContext.Current.Request.Cookies.Add(hc);
这是我检查cookie是否存在的地方. if (System.Web.HttpContext.Current.Request.Cookies["username"] != null) 这是所讨论方法的完整背景 public ActionResult LogOn()
{
if (System.Web.HttpContext.Current.Request.Cookies["username"] != null)
return View(new LogOnModel { UserName = System.Web.HttpContext.Current.Request.Cookies["username"].Value });
else
return View();
}
[HttpPost]
public ActionResult LogOn(LogOnModel model,string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName,model.Password))
{
HttpCookie hc = new HttpCookie("username",model.UserName);
hc.Expires = DateTime.Now.AddYears(1);
System.Web.HttpContext.Current.Request.Cookies.Add(hc);
FormsService.SignIn(model.UserName,model.RememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index","Home");
}
}
else
{
ModelState.AddModelError("","The user name or password provided is incorrect.");
}
}
return View(model);
}
解决方法添加到response.cookies not request.cookies(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 作为Windows服务托管的c#WCF Restful Web服务的
- ASP.NET Core 2.0 WebApi全局配置及日志实例
- msbuild – 通过TFS 2015部署ASP.NET 5(vNext)
- asp.net-mvc – ASP.NET MVC – Partial View可以有一个控制
- 使用ASP.NET窗体身份验证的WCF服务
- asp.net – 你对Windows Workflow Foundation有什么经验?
- asp.net – 我可以确定HttpModules是按照HttpApplication.M
- ASP.NET MVC 3 Ajax.BeginForm和Html.TextBoxFor不反映在服
- asp.net – 为什么removeServerHeader在Azure Web App中工作
- asp.net-core – asp.net核心依赖注入问题 – AddScoped没有
推荐文章
站长推荐
- asp.net-mvc – Html.HiddenFor值属性未设置
- 如何使用ASP.NET解析JSON字符串?
- asp.net-mvc – Asp.net MVC 3路由区域失败
- asp.net – 如何处理从“DBNull”类型到“String
- asp.net-mvc – ASP.NET MVC 2预览2:区域重复控
- asp.net – 从两个表(join)获取数据,并使用linq返
- asp.net – 带有iTextSharp的页眉,页脚和大表
- ASP.NET身份验证在自定义机票上滑动到期时间
- asp.net-mvc-3 – ViewBag.Title错误
- asp.net-web-api – 如何只获取没有值的Odata.Co
热点阅读
