【问题标题】:asp .net core app doesn't set response cookie in IE and EDGE but works well in firefox and chromeasp .net 核心应用程序不会在 IE 和 EDGE 中设置响应 cookie,但在 Firefox 和 chrome 中运行良好
【发布时间】:2018-11-14 11:56:47
【问题描述】:

我有一个登录控制器,它带有一个 post 操作,在成功登录后重定向到主页。我正在使用以下代码进行重定向,这在 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);

【问题讨论】:

    标签: c# asp.net asp.net-core-mvc


    【解决方案1】:

    由于您没有从Startup.cs 发布您的路线图,我不确定为什么它对您不起作用。也许您不应该通过将new { controller = "", action = "" } 传递给RedirectToRoute() 方法来覆盖controlleraction 参数?

    相反,您是否尝试过仅使用路由名称调用方法,例如

    return RedirectToRoute("default");
    

    或者,您可以使用RedirectToAction()

    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }
    
    // action, controller and route values
    return RedirectToAction("index", "home", new { area = "" });
    

    【讨论】:

      【解决方案2】:

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

      option.Domain = AppHost;
      

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 2014-12-25
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多