首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于FriendlyUrls,jQuery PageMethods 401身份验证失败

由于FriendlyUrls,jQuery PageMethods 401身份验证失败
EN

Stack Overflow用户
提问于 2013-11-30 21:22:54
回答 4查看 1.9K关注 0票数 1

我已经在WebForm应用程序中添加了FriendlyUrls nuget包。

在RegisterRoutes中,我有:

代码语言:javascript
复制
var settings = new FriendlyUrlSettings();
//settings.AutoRedirectMode = RedirectMode.Off; 
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);

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

在WebForm1.aspx中,我在head中引用了div v1.9.1,只是在body中的默认jQuery标记中添加了以下内容:

代码语言:javascript
复制
<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保持常规标准,但添加到代码后面的一个方法除外:

代码语言:javascript
复制
[System.Web.Services.WebMethod(EnableSession = false)]
        public static string GetCategories()
        {
            return "hi";
        }

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

代码语言:javascript
复制
{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}

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

代码语言:javascript
复制
http://localhost:14918/WebForm2.aspx/GetCategories

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

谁知道原因是什么,或者如何将排除添加到路由中?

我试着追随,但似乎不会以任何方式影响我不断获得的401结果:

代码语言:javascript
复制
//routes.Add(new Route("*Remote.aspx*", new StopRoutingHandler()));
 //routes.Ignore("{remote}", new { remote = @".*\Remote.aspx(/.)?" });
EN

回答 4

Stack Overflow用户

发布于 2016-03-13 08:03:40

你刚才保存的我的day.Below是c#版本的code.In,母版页只需粘贴PageMethods.set_path("default.aspx"),然后再关闭内容标签

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2014-11-04 22:17:54

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

代码语言:javascript
复制
$.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重定向方法设置,因此服务器正在接收来自未经身份验证的用户的请求。对于生产环境,请确保放置必要的安全细节,并接受来自经过身份验证的用户的请求,否则从代码后台暴露的方法可能会导致巨大的安全风险。

票数 1
EN

Stack Overflow用户

发布于 2015-05-20 18:47:45

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

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

代码语言:javascript
复制
<script>PageMethods.set_path("/Pages/Subjects.aspx")</script>

以下是VB中的示例代码:

代码语言:javascript
复制
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

希望这对某些人有帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20300757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档