none
使用FormsAuthenticationTicket 似乎有一个BUG?? RRS feed

  • 问题

  • 使用

    Response.Redirect(FormsAuthentication.GetRedirectUrl(username, chkRememberMe.Checked));//可以读出UserData 但是记不住登录状态!即 chkRememberMe.Checked 记住登录无效 

    使用 

    FormsAuthentication.RedirectFromLoginPage(username, chkRememberMe.Checked); //可以记住登录状态,却读不出UserData 

    登录方法,及相关帖子如下:

    http://social.microsoft.com/Forums/zh-CN/295/thread/08bcbce6-e59a-4362-ae5c-3e6bbd962e8e

     

        protected void BtnLogin_Click(object sender, EventArgs e)

        {

            string username = this.TxtUserName.Text;

            if (username.EndsWith("lkf", StringComparison.InvariantCultureIgnoreCase) && TxtUserPwd.Text.EndsWith("lkf", StringComparison.InvariantCultureIgnoreCase))

            {

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, 

    username,DateTime.Now,DateTime.Now.AddMinutes(30), true,  "admin|reader|editor", 

    FormsAuthentication.FormsCookiePath );

    string encTicket = FormsAuthentication.Encrypt(ticket);

       Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

    Response.Redirect(FormsAuthentication.GetRedirectUrl(username, chkRememberMe.Checked));//可以读出UserData 但是记不住登录状态! 

     //FormsAuthentication.RedirectFromLoginPage(username, chkRememberMe.Checked); //可以记住登录状态,却读不出UserData 

            }

            else

            {

                Response.Write("密码不正确");

            }

    }

     

    读取是在另一页面: 

      protected void Page_Load(object sender, EventArgs e)

        {

            if (Page.User.Identity.IsAuthenticated) //是否登录成功

            {

     

                FormsIdentity ident = User.Identity as FormsIdentity;

     

                Response.Write("Welcome back: " + Page.User.Identity.Name);

                Response.Write("<br>is admin? " + Page.User.IsInRole("admin"));

                Response.Write("<BR>UserData: " + ident.Ticket.UserData);

            }

        }

     


    www.haodd123.com 我是半路出家,而且是自学 学的是C#语言,希望回帖的高手们留意一下,谢谢大家的帮助
    2011年3月22日 18:42

答案