Jay*_*Jay 6 asp.net asp.net-mvc asp.net-identity
我使用ASP.NET Web应用程序模板创建了一个带有"身份验证:个人用户帐户"的新"单页应用程序".它将使用默认设置运行,没有任何问题.
如果我不将应用程序部署到Web服务器的根文件夹,则身份验证将失败.罪魁祸首在app.viewmodel.js文件中,可以找到以下代码:
self.addViewModel = function (options) {
var viewItem = new options.factory(self, dataModel),
navigator;
// Add view to AppViewModel.Views enum (for example, app.Views.Home).
self.Views[options.name] = viewItem;
// Add binding member to AppViewModel (for example, app.home);
self[options.bindingMemberName] = ko.computed(function () {
if (!dataModel.getAccessToken()) {
// The following code looks for a fragment in the URL to get the access token which will be
// used to call the protected Web API resource
var fragment = common.getFragment();
if (fragment.access_token) {
// returning with access token, restore old hash, or at least hide token
window.location.hash = fragment.state || '';
dataModel.setAccessToken(fragment.access_token);
} else {
// no token - so bounce to Authorize endpoint in AccountController to sign in or register
window.location = "/Account/Authorize?client_id=web&response_type=token&state=" + encodeURIComponent(window.location.hash);
}
}
return self.Views[options.name];
});
Run Code Online (Sandbox Code Playgroud)
其中,该生产线window.location = "/Account..."将浏览器重定向到URL一个在根目录偏移.不幸的是,只需将其硬编码到新文件夹(我想避免使用)并不能完全解决问题.
重定向似乎首先起作用,但是在AccountController.cs文件
的幕后Authorize()调用,反过来调用,AuthenticationManager.SignIn(identity)并且某些地方有魔法在继续.有一个重定向http://localhost/foo/Account/Login?ReturnUrl=...,我们回到了我们开始的地方.
我可能错过了显而易见的事实.我很感激任何指针.
这很容易复制.只需使用默认设置创建一个新的Web应用程序,然后进入项目属性并将"Project Url"更改为http://localhost:49725/foo将应用程序移动到名为"foo"的新文件夹.
我面临同样的问题,我所做的改变是:
在文件 app.viewmodel.js 中,我添加了一个新参数(returnUrl):
// no token - so bounce to Authorize endpoint in AccountController to sign in or register
window.location = "/Account/Authorize?client_id=web&response_type=token&state="
+ encodeURIComponent(window.location.hash)
+ "&returnUrl=" + encodeURIComponent(window.location);
Run Code Online (Sandbox Code Playgroud)ValidateClientRedirectUri在类的方法中ApplicationOAuthProvider,我读取此参数并将其设置为返回网址:
public override Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context)
{
if (context.ClientId == _publicClientId)
{
Uri expectedRootUri;
if (!string.IsNullOrEmpty(context.Request.Query["returnUrl"]))
{
expectedRootUri = new Uri(context.Request.Query["returnUrl"]);
}
else
{
expectedRootUri = new Uri(context.Request.Uri, "/");
}
if (expectedRootUri.AbsoluteUri == context.RedirectUri)
{
context.Validated();
}
else if (context.ClientId == "web")
{
var expectedUri = new Uri(context.Request.Query["returnUrl"]);
context.Validated(expectedUri.AbsoluteUri);
}
}
return Task.FromResult<object>(null);
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
1096 次 |
| 最近记录: |