如何获得在asp.net网站上的cookie值 [英] How to get the cookie value in asp.net website

查看:193
本文介绍了如何获得在asp.net网站上的cookie值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个cookie并succesfull登录后保存的用户名的值。如何在网页打开时,我可以访问该cookie。如果cookie存在,我想填补cookie值的用户名文本框。而如何解密来获取用户名的值。我从数据库中获取的UserDetails做服务器端验证。我使用VS 2010的C#

 的FormsAuthenticationTicket TKT;
串cookiestr;
CK的HttpCookie;
TKT =新的FormsAuthenticationTicket(1,txtUserName.Value,DateTime.Now,
    DateTime.Now.AddYears(1),chk_Rememberme.Checked,用户电子邮件);
cookiestr = FormsAuthentication.Encrypt(TKT);
CK =新的HttpCookie(FormsAuthentication.FormsCookieName,cookiestr);如果(chk_Rememberme.Checked)
{
    ck.Expires = tkt.Expiration;
    ck.Path = FormsAuthentication.FormsCookiePath;
    Response.Cookies.Add(CK);
}

饼干与名称创建为.YAFNET_Authentication和内容被加密

Webconfig:

 <形成NAME = loginUrl =Home.aspxYAFNET_Authentication。
  保护=所有超时=15000无Cookie =UseCookies/>


解决方案

您可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.htt$p$pquest.cookies.aspx\">Request.Cookies收集阅读饼干。

 如果(Request.Cookies时[钥匙]!= NULL)
{
   VAR值= Request.Cookies时[钥匙]值。
}

I am creating a cookie and storing the value of username after succesfull login. How can I access the cookie when the website is opened. If the cookie exist I want to fill the username text box from the cookie value. And how to decrypt the value to get the username. I am doing server side validation by getting the userdetails from the database. I am using vs 2010 with 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 is created with name as .YAFNET_Authentication and content is encrypted

Webconfig:

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

解决方案

You may use Request.Cookies collection to read the cookies.

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

这篇关于如何获得在asp.net网站上的cookie值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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