ibrahimerd
ibrahimerd

Reputation: 1

FormsAuthentication Request.Cookies returns null

I log in with FormsAuthentication. Remember Me is on. I added 6 months for the expiration date. After logging in, I also check cookies and see that the cookie is saved. Expire date is recorded 6 months later. It logs in normally, everything works fine, but when I try to log in the next day, it doesn't remember me and sends me back to the login screen.

I added the following line to the login page controller and I am checking cookies:

HttpCookie authCookie = HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];

authCookie always returns null the next day. When I go to developer tools from the browser and check the cookie, I see that the cookie is present. Why does HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName] return null?

I defined it as follows in Web Config:

<forms loginUrl="/Login" protection="All" slidingExpiration="true" timeout="8640" cookieless="UseCookies" name="FasonTakip"></forms>

Here is the code at Login/Index Controller :

HttpCookie authCookie = HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            if (authTicket == null || authTicket.Expired)
            {
                return View();
            }
            else
            {
                string _userData = authTicket.UserData;
                string code = _userData;

                var rs = db.User.Where(x => x.Code == code).FirstOrDefault();
                if (rs != null)
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    return View();
                }
            }
        }
        else
        {
            return View();

            } enter image description here

I tried to change slidingExpiration to false but didn't work.

Upvotes: 0

Views: 21

Answers (0)

Related Questions