Skip to main content
 首页 » 编程设计

c#之jQuery PageMethods 401 身份验证因 FriendlyUrls 而失败

2024年02月13日15lovecherry

我已将 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# 版本。如果是母版页,只需在关闭 Content 标签之前粘贴 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); 
            } 
        } 
    }