ASP.NET 身份验证“记住我”复选框未保持选中状态

问题描述 投票:0回答:3

我的登录页面出现问题。它从 cookie 中正确提取用户名,但是当我查看页面时,“记住我”的复选框不会被选中,即使正在执行在 Page_Load 上设置它的代码。

用于设置 cookie 的 LoggedIn 事件

    protected void lLogin_LoggedIn(object sender, EventArgs e)
    {
        // If Remember me then set an appropriate cookie
        if (lLogin.RememberMeSet)
        {
            HttpCookie loginCookie = new HttpCookie("loginCookie");
            Response.Cookies.Remove("loginCookie");
            Response.Cookies.Add(loginCookie);
            loginCookie.Values.Add("username", lLogin.UserName.ToString());
            DateTime dtExpiry = DateTime.Now.AddDays(15);
            Response.Cookies["loginCookie"].Expires = dtExpiry;
        }

        // Set a cookie to expire after 1 second
        else
        {
            HttpCookie loginCookie = new HttpCookie("loginCookie");
            Response.Cookies.Remove("loginCookie");
            Response.Cookies.Add(loginCookie);
            loginCookie.Values.Add("username", lLogin.UserName.ToString());
            DateTime dtExpiry = DateTime.Now.AddSeconds(1); //you can add years and months too here
            Response.Cookies["loginCookie"].Expires = dtExpiry;
        }
    }

登录页面的Page_Load事件

    protected void Page_Load(object sender, EventArgs e)
    {
        // Get username field to set focus
        TextBox txtUserName = (TextBox)lLogin.FindControl("UserName");

        if (!IsPostBack)
        {
            // For resetting the login url so that it doesn't have a return value in the URL
            if (Request.QueryString["ReturnURL"] != null)
            {
                Response.Redirect("~/Login.aspx", true);
            }


            if (Request.IsAuthenticated)
            {
                Response.Redirect("~/Main/Home.aspx", true);
            }

            // If login cookie exists pull username
            if (Request.Cookies["loginCookie"] != null)
            {
                HttpCookie loginCookie = Request.Cookies["loginCookie"];
                lLogin.UserName = loginCookie.Values["username"].ToString();
                CheckBox cb = (CheckBox)lLogin.FindControl("RememberMe");
                // This is being Executed which is why I am puzzled
                cb.Checked = true;
            }
        }

        this.SetFocus(txtUserName);         
    }

我的 Web.Config 包含以下信息以及 MachineKey,这是正确的吗?

    <authentication mode="Forms">
  <forms loginUrl="Login.aspx" protection="All" timeout="60000" name="HRKCO" slidingExpiration="true" />
</authentication>
    <sessionState mode="InProc"  cookieless="UseCookies" timeout="30"/>

编辑

我通过使用解决了这个问题:

lLogin.RememberMeSet = true;

我认为这与查找 RememberMe CheckBox 并设置选中状态相同,但显然事实并非如此。只是想如果其他人也遇到类似的问题,我会分享这个。

asp.net forms-authentication
3个回答
1
投票

forms
web.config
元素中,您是否尝试过
cookieless="UseCookies"
属性?我看到您拥有它用于
sessionState
,但我相信您也需要它用于
forms


0
投票

我的问题的解决方案是我在

Page_Load
事件中设置复选框的方式。

解决方案

// If login cookie exists pull username
    if (Request.Cookies["loginCookie"] != null)
    {
        HttpCookie loginCookie = Request.Cookies["loginCookie"];
        lLogin.UserName = loginCookie.Values["username"].ToString();
        lLogin.RememberMeSet = true;
    } 

0
投票

将 RememberMeSet="true" 设置为 asp:Login control

<asp:Login ID="Login1" runat="server" RememberMeSet="true" ... >
© www.soinside.com 2019 - 2024. All rights reserved.