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

发布于 2024-09-17 21:05:37 字数 2867 浏览 4 评论 0原文

我的登录页面出现问题。它从 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 并设置选中状态相同,但显然事实并非如此。只是想如果其他人也遇到类似的问题,我会分享这个。

I am having problems with my login page. It pulls through the Username correctly from the cookie, however the CheckBox for Remember Me does not become checked when I view the page, even though the code for setting it on the Page_Load is being executed.

LoggedIn event for setting the cookies

    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 event for Login page

    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);         
    }

My Web.Config contains the following information as well as a MachineKey, Is this correct?

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

EDIT

I solved this problem by using:

lLogin.RememberMeSet = true;

I assumed this would be the same as finding the RememberMe CheckBox and setting the checked state but obviously it was not. Just thought I would share this if anyone else was having similar problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

椒妓 2024-09-24 21:05:38

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

In the forms element of your web.config, have you tried the cookieless="UseCookies" attribute? I see that you have it for sessionState, but I believe you need it for forms too.

眼眸 2024-09-24 21:05:38

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

The Solution to my problem was the way in which I was setting the checkbox in the Page_Load Event.

Solution

// 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;
    } 
若能看破又如何 2024-09-24 21:05:38

将 RememberMeSet="true" 设置为 asp:Login 控件

<asp:Login ID="Login1" runat="server" RememberMeSet="true" ... >

Set RememberMeSet="true" to asp:Login control

<asp:Login ID="Login1" runat="server" RememberMeSet="true" ... >
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文