【问题标题】:URL encoding # sharp to Durandal SPAURL 编码 # 锐利到 Durandal SPA
【发布时间】:2014-04-03 09:12:09
【问题描述】:

我知道没有必要为 Durandal 登录页面指定登录 URL。但我想知道如何解决我面临的以下问题,将身份验证重定向到具有 Sharp 符号(即 #)的特定 Durandal 页面。

public void ConfigureAuth(IAppBuilder app)
{
     app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            //LoginPath = new PathString("/" + HttpUtility.UrlDecode("#") + "/login")
            LoginPath = new PathString("/#/login")
        });
     ...
}

当我粘贴时:

http://localhost/#/login

到浏览器 URL 我可以毫无问题地导航到登录页面。我可以登录,它工作正常。

因为我在某些场景中将 MVC 与 SPA 混合使用,所以当我将 [Authorize] 属性添加到 MVC 控制器时,我将按预期重定向到

http://localhost/%23/login?ReturnUrl=%2Fe

我得到了错误:

“/”应用程序中的服务器错误。

找不到资源。 说明:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下 URL 并确保其拼写正确。

请求的 URL:/#/login

如何使用 # 符号而不是 %23 字符编码? 或者我在搞砸别的东西!!!

仅供参考: 我的问题与字符编码有关,而不是这样:Login page on different domain,因为我将来在其他情况下可能会遇到同样的问题。

【问题讨论】:

  • 你试过去掉前面的 / 吗?
  • 是的,我已尝试删除前导 /,但这是不可能的。它清楚地要求我添加前导 /
  • 你自己解决过这个问题吗?我现在也有同样的问题。
  • @Cowman:我将“Sjoerd”标记为已回答。

标签: asp.net-mvc asp.net-web-api durandal


【解决方案1】:

据我所知,无法在服务器上使用 #。一种可能的解决方案是在用户未通过身份验证时从 durandal.js 中的 shell 重定向用户。

【讨论】:

  • 我可以使用 js+Durandal 轻松重定向客户端。我需要来自服务器的解决方案。如果不可能,我们可能会做一些我还没有发现的解决方法。
  • 在 MSDN 上查找 PathString 结构时,我发现您也可以从 uri 创建 PathString。 msdn.microsoft.com/en-us/library/dn447917(v=vs.111).aspx
【解决方案2】:

我不知道在生命周期中是否为时已晚,但您可以尝试在 Application_EndRequest 事件中捕获全局 asax 文件中的重定向并修复响应。我有这样的代码,可以将 actionresult 更改为我的 js 可以使用的 jsonresult,以便当用户尝试访问我通过 java script ajax 保护的 MVC 控制器资源时,我可以正确处理它。

protected void Application_EndRequest()
    {
        var context = new HttpContextWrapper(this.Context);

        // If we're an ajax request and forms authentication caused a 302, 
        // then we actually need to do a 401
        if (FormsAuthentication.IsEnabled && context.Response.StatusCode == 302
            && context.Request.IsAjaxRequest())
        {
            var jsRet = new JSONFunctionResult<Boolean>();

            if (this.Request.IsAuthenticated)
            {
                jsRet.Messages.Add(new JSONFunctionResultMessage()
                {
                    Text = string.Format("You must have one of these roles to perform the operation: {0}", context.Response.Headers["RolesRequired"]),
                    Title = "Security Violation",
                    Type = (int)JSONFunctionResultMessageTypes.authorization
                });
            }
            else
            {
                jsRet.Messages.Add(new JSONFunctionResultMessage()
                {
                    Text = "You must be logged into to access this resource",
                    Title = "Security Violation",
                    Type = (int)JSONFunctionResultMessageTypes.authentication
                });
            }
            jsRet.OperationStatus = JSONFunctionResultOperationStatus.error.ToString();

            string jresponse = JsonConvert.SerializeObject(jsRet);

            context.Response.Clear();
            context.Response.StatusCode = 200;
            context.Response.ContentType = "application/json";
            context.Response.Write(jresponse);
        }
    }

在我的代码中,我检查重定向以及是否启用了表单身份验证以及用户是否已通过身份验证并相应地更改响应。对于您的代码,您可能会更改响应以转到正确的登录页面?

【讨论】:

  • 谢谢。请给我一些时间来检查这个有希望的。同时,我的问题与身份验证无关。请根据您的经验告诉我是否可以从服务器使用#?
  • 再次查看您的帖子后,我看到了 IAppBuilder 代码,这意味着您正在使用 owin。抱歉,我没有这方面的经验,我的建议是基于 asp.net MVC,不确定您是否可以通过 web.config 文件配置它,也许将其设置为字符串可能会导致您得到不同的结果。跨度>
猜你喜欢
  • 1970-01-01
  • 2013-06-21
  • 2021-08-28
  • 1970-01-01
  • 2011-04-17
  • 2017-03-08
  • 2013-06-03
  • 2014-05-10
  • 2017-12-01
相关资源
最近更新 更多