编程语言
首页 > 编程语言> > c#-asp .net核心应用未在IE和EDGE中设置响应cookie,但在Firefox和chrome中效果很好

c#-asp .net核心应用未在IE和EDGE中设置响应cookie,但在Firefox和chrome中效果很好

作者:互联网

我有一个具有发布操作的登录控制器,该登录操作在成功登录后重定向到主页.我正在使用以下代码进行重定向,该代码在Chrome和Firefox中效果很好.但不会在IE和EDGE中重定向,并且未设置响应cookie

private ActionResult RedirectToLocal(string returnUrl)
{
    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToRoute("default", new { controller = "", 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();
    }

我正在使用自己的会话管理,为此我设置了会话Cookie,如下所示

CookieOptions option = new CookieOptions();
option.Path = AppPath;
option.Domain = AppHost;             
httpContextAccessor.HttpContext.Response.Cookies.Append(AppSessionToken, "SomeSessionId", option);

解决方法:

在搜索了很多确切的答案之后,我发现Internet Explorer(所有版本)不允许您指定localhost的域,本地IP地址或计算机名称.当您这样做时,Internet Explorer只是忽略cookie.
所以我删除了以下行

option.Domain = AppHost;

从我的代码开始,一切都开始在IE和EDGE上按预期工作.

标签:asp-net-core-mvc,asp-net,c
来源: https://codeday.me/bug/20191109/2010595.html