Cookie不适用于“记住我"复选框asp.net [英] Cookie not working for remember me checkbox asp.net

查看:96
本文介绍了Cookie不适用于“记住我"复选框asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施记住我"复选框,以实现我实施的密码,例如

i am implementing remember me checkbox for password i implemented like

if (TextBox1.Text == "user" && TextBox2.Text == "user")
       {
           Response.Redirect("Default2.aspx");
       }
       if (CheckBox1.Checked)
       {
           HttpCookie cookie = new HttpCookie("username");
           cookie.Value = TextBox2.Text;
           cookie.Expires = DateTime.Now.AddHours(2);
           HttpContext.Current.Response.AppendCookie(cookie);
       }
       else
       {
           HttpContext.Current.Response.Cookies.Remove("username");
           Response.Cookies["username"].Expires = DateTime.Now;
       }


但任何人都无法正常工作,请知道帮我


but not working can any body know please help me

推荐答案

this[^] blog might help you.


为什么要重新发明轮子?您可以通过使用FormsAuthentication来实现. ASP.NET开箱即用地提供了此功能....请参见以下链接

http://msdn.microsoft.com/en-us/library/xdt4thhy.aspx [ ^ ]
Why to re-inventing the wheel? You can achive this by using FormsAuthentication. ASP.NET provides this functionality out of the box....see the following link

http://msdn.microsoft.com/en-us/library/xdt4thhy.aspx[^]


您需要更改代码.
请使用以下代码.希望它能正常工作.如果不满足您的要求,请留下您的评论.
You need to change your code.
Please use the following code.Hope it will work.If doesn''t fulfill your requirement, please fell free to leave your comment.
try
       {
           if (!String.IsNullOrEmpty(TextBox1.Text) && !String.IsNullOrEmpty(TextBox2.Text))
           {
               if (CheckBox1.Checked)
               {
                   if (Request.Cookies["login"] != null)
                   {
                       HttpCookie getCookie = Request.Cookies.Get("login");
                       Response.Redirect("Default2.aspx?UserName : ''" + getCookie.Values["UserName"].ToString() + "'' & Password : ''" + getCookie.Values["Password"].ToString() + "''");
                   }
                   else
                   {
                       HttpCookie cookie = new HttpCookie("login");
                       Response.Cookies.Add(cookie);
                       cookie.Values.Add("UserName", TextBox1.Text.ToString());
                       cookie.Values.Add("Password", TextBox2.Text.ToString());
                       cookie.Expires = DateTime.Now.AddHours(2);
                   }
               }
               else
               {
                   HttpContext.Current.Response.Cookies.Remove("login");
                   Response.Cookies["login"].Expires = DateTime.Now;
               }


           }
       }
       catch (Exception ex)
       {
           ex.ToString();
       }


如果这对您真的很有帮助,请不要忘记投票并接受答案.


这篇关于Cookie不适用于“记住我"复选框asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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