我正在创建一个cookie,并在成功登录后存储用户名的值.当网站打开时,我如何访问cookie.如果cookie存在,我想从cookie值填充用户名文本框.以及如何解密该值以获取用户名.我通过从数据库获取用户详细信息来进行服务器端验证.我使用VS2010和c#

FormsAuthenticationTicket tkt;
string cookiestr;
HttpCookie ck;
tkt = new FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now,
    DateTime.Now.AddYears(1), chk_Rememberme.Checked, "User Email");
cookiestr = FormsAuthentication.Encrypt(tkt);
ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);

if (chk_Rememberme.Checked)
{
    ck.Expires = tkt.Expiration;
    ck.Path = FormsAuthentication.FormsCookiePath;
    Response.Cookies.Add(ck);
}

cookie的名称为.YAFNET_身份验证和内容加密

网络配置:

  <forms name=".YAFNET_Authentication" loginUrl="Home.aspx"
  protection="All" timeout="15000" cookieless="UseCookies"/>

推荐答案

您可以使用Request.Cookies个集合来阅读cookies.

if(Request.Cookies["key"]!=null)
{
   var value=Request.Cookies["key"].Value;
}

Asp.net相关问答推荐

Razor 页面客户端站点验证不起作用

Web 部署工具 2.1 和 Web 部署 3.5 有什么区别?从 VS 2010 部署需要哪一个?

格式化 DataBinder.Eval 数据

如何在 GridView 单元格中的(即
)中呈现解码的 HTML

任何人都有解决 Internet Explorer 上剩余 n 项问题的 idea 吗?

HttpResponse 的区别:SetCookie、AppendCookie、Cookies.Add

相当于服务器端的 ASP.NET 包括

没有回发的按钮?

aspx 文件中的 if 语句

ASP.NET 中的 <% %>(嵌入式代码块)

LINQ to Entities 无法识别 MVC 4 中的System.String ToString()方法

页面在谷歌浏览器中加载两次

DropDownList AppendDataBoundItems(第一项为空白且无重复项)

这个rendersection的代码是什么意思?

ASP.NET Core 中的 NuGet 包位置在哪里?

调查 Web.Config 重复部分原因的步骤

C# - 将图像输出到响应输出流给出 GDI+ 错误

将 HTML 字符串转换为图像

如何在不遍历每个循环的情况下从字典对象中获取所有键(仅键)

如何创建代表 colored颜色 的随机十六进制字符串?