【问题标题】:ASP.NET Authentication "Remember Me" CheckBox not staying checkedASP.NET 身份验证“记住我”复选框未保持选中状态
【发布时间】:2010-09-07 15:22:54
【问题描述】:

我的登录页面有问题。它正确地从 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 并设置选中状态相同,但显然不是。只是想如果其他人有类似的问题,我会分享这个。

【问题讨论】:

  • 不检查?还是没有正确获取校验值?
  • lLogin 是登录控件。它没有保持检查 - 即使 cb.CChecked = true :(

标签: asp.net forms-authentication


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我的问题的解决方案是我在 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;
        } 
    

    【讨论】:

    • 我打算暗示这可能是预期的行为。如果用户拥有 cookie,这意味着他们已经选中复选框并登录。理想情况下,用户将永远不会再次看到登录提示/记住我复选框(因为他们应该使用 cookie 自动登录)。除非用户手动点击注销。在这种情况下,将不存在现有的 login cookie,因为它会在注销时被删除,并且下次他们尝试登录时,该复选框将(并且应该)被取消选中。
    • 一开始我也很困惑。 我发誓我已经勾选了复选框。然后我意识到我再次看到复选框的唯一原因是因为我没有登录。
    猜你喜欢
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    相关资源
    最近更新 更多