jQuery PageMethods 401身份validation失败,使用FriendlyUrls

我将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,只需在body中的默认div标签内添加以下内容:

 
$(function() { $.fpm("GetCategories", '', function (res) { $("div#dvResult").html(res.d); }, function (xhr, ajaxOptions, thrownError) { $("div#dvResult").html("" + thrownError + "
Status: " + xhr.status + "
" + 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 }); };

将文件添加到项目后,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 out时,您实际上会在屏幕上显示结果“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); } } } 

这是迟到的,但万一有人有同样的问题。 简单修复,设置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重定向方法设置,因此服务器正在接收来自未经身份validation的用户的请求。 对于生产,请确保提供必要的安全性详细信息并接受来自经过身份validation的用户的请求,否则代码中暴露的方法可能会导致巨大的安全风险。

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

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

  

这是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 

希望有人帮助!

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