简体   繁体   中英

Unable to authenticate user manually

I have the following code on my login page:

    if (authentication method returns true)
    {
        FormsAuthenticationTicket ticket;

        if (cbRemember.Checked)
        {
            ticket = new FormsAuthenticationTicket(1, tbUsername.Text, DateTime.Now, DateTime.Now.AddYears(1), true, null, FormsAuthentication.FormsCookiePath);
        }
        else
        {
            ticket = new FormsAuthenticationTicket(1, tbUsername.Text, DateTime.Now, DateTime.Now.AddHours(1), true, null, FormsAuthentication.FormsCookiePath);
        }

        string encryptedTicket = FormsAuthentication.Encrypt(ticket);

        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
        cookie.HttpOnly = true;

        Response.Cookies.Add(cookie);

    }
    else
    {
        errorBox.Visible = true;
    }

However the user is still not authenticated after this. Am I doing it wrong?

The problem was that I was passing null into one of the ticket's parameters. You have to pass string.empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM