Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp .net core app doesn't set response cookie in IE and EDGE but works well in firefox and chrome

I have a login controller with a post action which redirects to home page after successful login. I am using following code for redirection, which works well in Chrome and Firefox. but doesn't redirect in IE and EDGE, and response cookie not set

private ActionResult RedirectToLocal(string returnUrl)
{
    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToRoute("default", new { controller = "", action = "" });
    }
}

My Login action

public IActionResult Login(string userName, string password)
{
   try
   {
       if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
                throw new InvalidOperationException("Username/Password can not be empty");

            var session = CreateSession(userName, password);
            var returnUrl = Request.Query["returnUrl"];
            return RedirectToLocal(returnUrl);
    }
    catch (Exception ex)
    {
        ModelState.AddModelError("Login", ex.Message);
    }

        return View();
    }

I am using my own session management for which I set session cookies like following

CookieOptions option = new CookieOptions();
option.Path = AppPath;
option.Domain = AppHost;             
httpContextAccessor.HttpContext.Response.Cookies.Append(AppSessionToken, "SomeSessionId", option);
like image 755
Tushar Avatar asked Jun 05 '18 05:06

Tushar


1 Answers

After searching a lot for exact answer, I found that Internet Explorer (all versions) doesn't allow you to specify a domain of localhost, a local IP address or machine name. When you do, Internet Explorer simply ignores the cookie. So I removed following line

option.Domain = AppHost;

from my codes and everything start working as expected on both IE and EDGE.

like image 189
Tushar Avatar answered Oct 12 '22 23:10

Tushar