通常这没什么大不了的,但我正在构建一个 AngularJS 应用程序,而 ASP.NET 的身份外部登录(基于 OWIN)添加的 # 正在对 angularjs 应用程序造成严重破坏。
您甚至可以在默认的新 C# ASP.NET MVC 站点上看到它,Google 在 Startup.Auth.cs 中取消注释。
当用户登录并重定向时,# 被添加到 url 的末尾。我已经尝试自己处理此重定向,但无济于事......总是添加哈希标签。
有什么想法吗?提前致谢。
更新
默认解决方案使用表单来启动外部登录,如下所示:
using (Html.BeginForm(action, "Account", new { ReturnUrl = returnUrl }))
{
@Html.AntiForgeryToken()
<div id="socialLoginList">
<p>
@foreach (AuthenticationDescription p in loginProviders)
{
<button type="submit" class="btn btn-default" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.AuthenticationType</button>
}
</p>
</div>
}
我没有看到在请求中添加 # 有什么意义。
如果您查看登录操作,您会看到它调用自己的私有(private)方法来处理重定向:
private ActionResult RedirectToLocal(string returnUrl) {
if (Url.IsLocalUrl(returnUrl)) {
return Redirect(returnUrl);
}
else {
return RedirectToAction("Index", "Home");
}
}
即使 return RedirectToAction("Index", "Home");
被命中,它也会在 url 末尾有一个 #。
我试过像这样对返回进行硬编码:return Redirect("/home/index");
但最后仍然有#。
更新 2 看起来它只发生在 Chrome 中。
最佳答案
简短的回答
据我所知你不能。
我遇到了完全相同的问题(尽管在所有浏览器中,而不仅仅是 Chrome)。当我调查时,我发现哈希是硬编码在 OAuthAuthorizationServerHandler.ApplyResponseGrantAsync 中的。方法,代码如下
var appender = new Appender(location, '#');
appender
.Append(Constants.Parameters.AccessToken, accessToken)
.Append(Constants.Parameters.TokenType, Constants.TokenTypes.Bearer);
较长的答案
要更改它的事件,请对我提出的问题进行投票: Provide a mechanism to choose the hash '#' separator when redirecting after an external login .
解决方法
我通过选择一个不是将 # 转换为 Hashbang #!
的 AngularJS 应用程序的着陆页解决了这个问题。这是我使用过的页面:
<!DOCTYPE html>
<html lang="en">
<head>
<script>window.location.replace('/#!/' + window.location.hash);</script>
<meta charset="utf-8">
<title>Please wait.</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>Please wait.</body>
</html>
希望这对您有所帮助。
关于c# - 如何防止 ASP.NET Identity 外部登录在 url 末尾添加#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21677061/