jQuery PageMethods 401身份验证失败,使用FriendlyUrls

时间:2013-11-30 13:22:54

标签: c# asp.net jquery webforms

我将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 out时,您实际得到结果&#34;嗨&#34;印在屏幕上。

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

我试过跟随,但它似乎没有影响我一直得到的401结果:

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

4 个答案:

答案 0 :(得分:2)

你刚刚保存了我的一天。下面是代码的c#版本。如果母版页只是在关闭内容代码之前粘贴PageMethods.set_path(&#34; default.aspx&#34;)

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);
            }
        }
    }

答案 1 :(得分:1)

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

答案 2 :(得分:1)

面对从大型已建立的应用程序中剥离大量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

希望能帮助别人!

答案 3 :(得分:0)

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

相关问题