【问题标题】:jQuery PageMethods 401 Authentication failed with FriendlyUrlsjQuery PageMethods 401 身份验证失败,FriendlyUrls
【发布时间】:2013-11-30 13:22:54
【问题描述】:

我已将 FriendlyUrls nuget 包添加到 WebForm 应用程序。

在 RegisterRoutes 我有:

var settings = new FriendlyUrlSettings();
//settings.AutoRedirectMode = RedirectMode.Off; 
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);

我创建了 2 个页面 WebForm1.aspx 和 WebForm2.aspx

在 WebForm1.aspx 上,我在头部引用了 jQuery v1.9.1,只是在正文的默认 div 标签内添加了以下内容:

<div id="dvResult"></div>

    <script type="text/javascript">

        $(function() {
            $.fpm("GetCategories", '', function (res) {
                $("div#dvResult").html(res.d);
            }, function (xhr, ajaxOptions, thrownError) {
                $("div#dvResult").html("<b>" + thrownError + "</b><br/>Status: " + xhr.status + "<br/>" + xhr.responseText);
            });
        });


        $.fpm = function fpm(methodName, arguments, onSuccess, onError) {
            var proto = (("https:" == document.location.protocol) ? "https://" : "http://");
            var hostname = window.location.hostname;
            if (window.location.port != 80)
                hostname = window.location.hostname + ":" + window.location.port;
            var loc = proto + "" + hostname + "/WebForm2.aspx";
            $.ajax({
                type: "POST",
                url: loc + "/" + methodName,
                data: "{" + arguments + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: onSuccess,
                error: onError
            });
        };

    </script>

WebForm2.aspx在将文件添加到项目后保持标准,除了在后面的代码中添加了1个方法:

[System.Web.Services.WebMethod(EnableSession = false)]
        public static string GetCategories()
        {
            return "hi";
        }

当我运行 WebForm1.aspx 页面时,我得到以下结果:

{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}

在 fiddler 中查看请求时,我可以看到友好的 url 没有去除 .aspx 扩展名(这是一件好事):

http://localhost:14918/WebForm2.aspx/GetCategories

但是,如上所示,FriendlyUrlSettings 将 AutoRedirectMode 设置为 RedirectMode.Permanent,当您取消注释 RedirectMode.Off 行并将 Permanent 注释掉时,您实际上会在屏幕上打印结果“Hi”。

任何人有任何想法可能是什么原因或如何为路线添加排除项?

我尝试过关注,但它似乎不会以任何方式影响我不断得到的 401 结果:

//routes.Add(new Route("*Remote.aspx*", new StopRoutingHandler()));
 //routes.Ignore("{remote}", new { remote = @".*\Remote.aspx(/.)?" });

【问题讨论】:

    标签: c# asp.net jquery webforms


    【解决方案1】:

    你刚刚拯救了我的一天。下面是代码的 c# 版本。如果是母版页,只需在关闭内容标记之前粘贴 PageMethods.set_path("default.aspx")

    public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings,  new CustomFriendlyUrlResolver());
        }
    
        public class CustomFriendlyUrlResolver : WebFormsFriendlyUrlResolver
        {
            public override string ConvertToFriendlyUrl(string path)
            {
    
                if (HttpContext.Current.Request.PathInfo != "")
                {
                    return path;
                }
                else
                {
                    return base.ConvertToFriendlyUrl(path);
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      这已经晚了,但万一有人遇到同样的问题。简单修复,设置 RedirectMode.Off 而不是 RedirectMode.Permanent。对于 Ajax 部分,对 url 键执行以下操作:

      $.ajax({
          type: "POST",
           url:'<%=ResolveUrl("sample.aspx/methodname")%>'
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (msg) {
                    alert("Worked");
                 },
             failure: function (msg) {
                    alert("Failed");
                 }
      });
      

      我有类似的问题,上述解决方案对我有用。这是一个快速修复,但我不建议将其用于生产。发生此错误的部分原因是 FriendlyUrl 与非 FriendlyUrl 重定向方法设置,因此服务器正在接收来自未经身份验证的用户的请求。对于生产环境,请确保设置必要的安全细节并接受来自经过身份验证的用户的请求,否则隐藏在代码中的方法可能会导致巨大的安全风险。

      【讨论】:

        【解决方案3】:

        面对从大型已建立的应用程序中剥离大量 PageMethods,我找到了以下切换到 WebApi 的替代解决方案(关闭 AutoRedirectMode 仍然允许在直接请求时显示我的文件扩展名,我真的不希望那样)。

        改为在您的 App_Start/RouteConfig 文件中使用自定义的 FriendlyUrls.Resolver。对现有页面的唯一更改是使用 PageMethods 向每个页面添加以下标记:

        <script>PageMethods.set_path("/Pages/Subjects.aspx")</script>
        

        这是VB中的示例代码:

        Imports Microsoft.AspNet.FriendlyUrls
        Imports Microsoft.AspNet.FriendlyUrls.Resolvers
        
        Public Module RouteConfig
            Sub RegisterRoutes(ByVal routes As RouteCollection)
                routes.EnableFriendlyUrls(New FriendlyUrlSettings() With {.AutoRedirectMode = RedirectMode.Permanent}, New IFriendlyUrlResolver() {New CustomFriendlyUrlResolver()})
            End Sub
        End Module
        
        Public Class CustomFriendlyUrlResolver
            Inherits WebFormsFriendlyUrlResolver
        
            Public Overrides Function ConvertToFriendlyUrl(path As String) As String
                If HttpContext.Current.Request.PathInfo <> "" Then Return path Else Return MyBase.ConvertToFriendlyUrl(path)
            End Function
        End Class
        

        希望对某人有所帮助!

        【讨论】:

          【解决方案4】:

          最终创建了 WebApi 项目,并在遇到一些新问题(与 CORS 相关)后,让它工作起来,实际上觉得它可能是比 pagemethods 更好的解决方案。

          【讨论】:

            猜你喜欢
            • 2021-06-14
            • 1970-01-01
            • 1970-01-01
            • 2013-10-04
            • 2013-09-22
            • 2014-01-24
            • 2017-11-27
            相关资源
            最近更新 更多