jQuery的PageMethods 401身份验证失败,FriendlyUrls(jQuery P

2019-09-29 14:56发布

我有NuGet包添加到Web窗体应用程序FriendlyUrls。

在我的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"}

当小提琴手查看请求我可以看到友好的URL没有剥离.aspx扩展(这是一件好事):

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

但是如上图所示,该FriendlyUrlSettings有AutoRedirectMode设置为RedirectMode.Permanent,当你取消注释RedirectMode.Off行和评论常驻出来,那么你实际上得到“喜”打印屏幕上的结果。

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

我曾尝试以下,但它似乎并没有以任何方式401结果我不断收到影响:

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

Answer 1:

你刚才救了我day.Below是母版页的code.In案件的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);
            }
        }
    }


Answer 2:

这晚,但万一有人有同样的问题。 简单的修复,设置RedirectMode.Off代替RedirectMode.Permanent的。 对于阿贾克斯的部分做的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重定向方法设置,因此在服务器接收到来自未认证的用户的请求。 对于生产,确保落实到位必要的安全信息,并接受身份验证的用户请求,否则从代码的背后暴露的方法可能会导致巨大的安全隐患。



Answer 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

希望可以帮助别人!



Answer 4:

结束了创建的WebAPI项目和之后的几个新问题到达(CORS相关的),得到它的工作,实际感受到它可能比pagemethods一个更好的解决方案。



文章来源: jQuery PageMethods 401 Authentication failed with FriendlyUrls