开发者

Asp.Net 4 Response.Cookies.Add does not add cookie to users machine

I am trying to setup a basic Form Authentication using ASP.NET 4.

I know my validation code (code that checks if the username and password is correct) is working because after if the user enters invalid information the ReturnLable tells them so. However if they enter the correct information, they are redirected to the restricted page with a 403 – Forbidden error. When I check the shell:cookie path no cookie has been written even though I added it to the collection “Response.Cookies.Add(cookie);”

protecte开发者_运维百科d void Submit_Click(object sender, EventArgs e)
{
    Email.Text = Email.Text.Trim();
    Password.Text = Password.Text.Trim();
    if (IsValid(Email.Text, Password.Text)) //user exists
    {
        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
            1,
            Email.Text,
            DateTime.Now,
            DateTime.Now.AddMinutes(50),
            RememberMe.Checked,
            "user",
            FormsAuthentication.FormsCookiePath);
        string hashCookies = FormsAuthentication.Encrypt(ticket);
        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);

        Response.Cookies.Add(cookie);
}
    else
    {
        ReturnLable.Text = "<font color=red> Username/Password Incorrect Please Try Again </font>";
        ReturnLable.Visible = true;
    }


From this MSDN article:

If you do not set the cookie's expiration, the cookie is created but it is not stored on the user's hard disk. Instead, the cookie is maintained as part of the user's session information. When the user closes the browser or if the session times out, the cookie is discarded.

Thus, a cookie could be successfully set, alive and well in the browser, but have no corresponding file in the "cookies" folder on the hard drive.


make sure that Enable anonymous access is disabled on IIS and Integrated Windows security is enabled

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜