加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

登录到MVC 5 ASP.NET模板从根文件夹移动时Web应用程序失败

发布时间:2020-12-16 03:51:51 所属栏目:asp.Net 来源:网络整理
导读:我使用ASP.NET Web应用程序模板创建了一个带有“身份验证:个人用户帐户”的新“单页应用程序”.它将使用默认设置运行,没有任何问题. 如果我不将应用程序部署到Web服务器的根文件夹,则身份验证将失败.罪魁祸首在app.viewmodel.js文件中,可以找到以下代码: s
我使用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];
    });

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);

>在ApplicationOAuthProvider类的方法ValidateClientRedirectUri中,我读取了此参数并设置为返回URL:

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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读