jQuery PageMethods 401身份验证失败,并显示FriendlyUrls [英] jQuery PageMethods 401 Authentication failed with FriendlyUrls

查看:133
本文介绍了jQuery PageMethods 401身份验证失败,并显示FriendlyUrls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将FriendlyUrls nuget包添加到了WebForm应用程序中.

I have FriendlyUrls nuget package added to WebForm application.

在RegisterRoutes中,我有:

In RegisterRoutes I have:

var settings = new FriendlyUrlSettings();
//settings.AutoRedirectMode = RedirectMode.Off; 
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);

我创建了2页WebForm1.aspx和WebForm2.aspx

I created 2 pages WebForm1.aspx and WebForm2.aspx

在WebForm1.aspx上,我在头引用了jQuery v1.9.1,只是在主体的默认div标记内添加了以下内容:

On WebForm1.aspx I referenced jQuery v1.9.1 in the head simply added the following inside the default div tag in the body:

<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保持标准状态,但在背后的代码中添加了一种方法:

WebForm2.aspx is kept stock standard after adding the file to the project, except for 1 method added to the code behind:

[System.Web.Services.WebMethod(EnableSession = false)]
        public static string GetCategories()
        {
            return "hi";
        }

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

When I run the page WebForm1.aspx I get the following result:

{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}

在提琴手中查看请求时,我可以看到友好的URL并未剥离.aspx扩展名(这是一件好事):

When view the request in fiddler I can see the friendly url did not strip the .aspx extension (which is a good thing):

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

但是,如上所示,FriendlyUrlSettings将AutoRedirectMode设置为RedirectMode.Permanent,并且当您取消注释RedirectMode.Off的行并注释掉Permanent时,实际上您会在屏幕上得到结果"Hi".

However as shown above, the FriendlyUrlSettings has the AutoRedirectMode set to RedirectMode.Permanent and when you uncomment the line for RedirectMode.Off and comment the Permanent out, then you actually get the result "Hi" printed on the screen.

任何人都知道原因可能是什么,或如何在路由中添加排除项?

Anyone has any ideas what the cause could be or how to add an exclusion to the routes?

我已尝试遵循,但它似乎并不会以任何方式影响我不断获得的401结果:

I have tried to following but it does not seem to affect in any way the 401 result I keep getting:

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

推荐答案

您刚刚保存了我的一天.下面是代码的C#版本.对于母版页,只需粘贴PageMethods.set_path("default.aspx")在关闭内容标签之前

You just saved my day.Below is the c# version of the code.In case of the master pages just paste PageMethods.set_path("default.aspx") before closing Content tag

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

这篇关于jQuery PageMethods 401身份验证失败,并显示FriendlyUrls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆