Tus*_*har 5 c# asp.net asp.net-core-mvc
我有一个带有 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 = "" });
}
}
Run Code Online (Sandbox Code Playgroud)
我的登录操作
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();
}
Run Code Online (Sandbox Code Playgroud)
我正在使用我自己的会话管理,为此我设置了如下会话 cookie
CookieOptions option = new CookieOptions();
option.Path = AppPath;
option.Domain = AppHost;
httpContextAccessor.HttpContext.Response.Cookies.Append(AppSessionToken, "SomeSessionId", option);
Run Code Online (Sandbox Code Playgroud)
在大量搜索确切答案后,我发现 Internet Explorer(所有版本)不允许您指定 localhost 域、本地 IP 地址或机器名称。当您这样做时,Internet Explorer 会简单地忽略 cookie。所以我删除了以下行
option.Domain = AppHost;
Run Code Online (Sandbox Code Playgroud)
从我的代码开始,一切都在 IE 和 EDGE 上按预期工作。