通话过程中的WebMethod身份验证失败从jquery.ajx与AspNet.FriendlyUrls和AspNet.Identity [英] Authentication failed during call webmethod from jquery.ajx with AspNet.FriendlyUrls and AspNet.Identity

查看:2160
本文介绍了通话过程中的WebMethod身份验证失败从jquery.ajx与AspNet.FriendlyUrls和AspNet.Identity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我打电话的WebMethod从jQuery.Ajax安装的NuGet包Microsoft.AspNet.FriendlyUrls v 1.0.2和Microsoft.AspNet.Identity v.1.0.0,然后我得到的数据对象,但没有data.d但财产信息身份验证失败。

我的Webmethod.aspx是:

 <!DOCTYPE HTML>
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8/>
     <冠军>的WebMethod< /标题>
    <脚本SRC =脚本/ jQuery的-2.0.3.js>< / SCRIPT>
< /头>
<身体GT;
    <表格ID =Form1的=服务器>
    < H3>试验WEBMETHOD< / H3>
    < D​​IV ID =greeitng>< / DIV>
    < D​​IV ID =innerError的风格=边界:1px的红色虚线;显示:无;标题=的errorMessage>< / DIV>
    <脚本类型=文/ JavaScript的>
        功能asyncServerCall(用户名){
            jQuery.ajax({
                网址:WebMethod.aspx /的HelloWorld,
                键入:POST,
                数据:{用户名:+用户名+'},
                //异步:​​假的,
                的contentType:应用/ JSON的;字符集= UTF-8,
                数据类型:JSON,
                成功:功能(数据){
                    如果(data.d ==未定义)
                        的document.getElementById(greeitng)的innerHTML = data.Message。
                    其他
                        的document.getElementById(greeitng)的innerHTML = data.d。
                },
                错误:函数(ERR){
                    如果(err.responseText){
                        $('#innerError)HTML(err.responseText).show();
                    }
                    其他 {
                        警报(ERR);
                    }
                }
            });
        }
        $(文件)。就绪(函数(){
            $('#innerError)隐藏()。
            asyncServerCall(超级用户);
        });
    < / SCRIPT>
    < /形式GT;
< /身体GT;
< / HTML>
 

我在WebMethod.aspx.cs WEBMETHOD是:

  [System.Web.Services.WebMethod]
公共静态字符串的HelloWorld(字符串的用户名)
{
    返回的String.Format(您好,{0},用户名);
}
 

在的Global.asax.cs IS路由激活

 无效的Application_Start(对象发件人,EventArgs的)
{
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}
 

在App_Start已regitred路线

 公共静态类RouteConfig
{
    公共静态无效的RegisterRoutes(RouteCollection路由)
    {
        VAR设置=新FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(设置);
    }
}
 

解决方案

在你App_Start文件夹,您RouteConfig内....

注释掉以下行或改变它的RedirectMode:

  // settings.AutoRedirectMode = RedirectMode.Permanent;
 

If I call webmethod from jQuery.Ajax with installed Nuget packages Microsoft.AspNet.FriendlyUrls v 1.0.2 and Microsoft.AspNet.Identity v.1.0.0., then I get the data object, but without data.d but with property Message 'Authentication failed'.

My Webmethod.aspx is:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
     <title>WebMethod</title>
    <script src="Scripts/jquery-2.0.3.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <h3>Test Webmethod</h3>
    <div id="greeitng"></div>
    <div id="innerError" style="border:1px dotted red;display:none;" title="errorMessage"></div>
    <script type="text/javascript">
        function asyncServerCall(username) {
            jQuery.ajax({
                url: 'WebMethod.aspx/HelloWorld',
                type: "POST",
                data: "{'username':'" + username + "'}",
                //async: false,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if (data.d == undefined)
                        document.getElementById("greeitng").innerHTML = data.Message;
                    else
                        document.getElementById("greeitng").innerHTML = data.d;
                },
                error: function (err) {
                    if (err.responseText) {
                        $('#innerError').html(err.responseText).show();
                    }
                    else {
                        alert(err);
                    }
                }
            });
        }
        $(document).ready(function () {
            $('#innerError').hide();
            asyncServerCall("Superuser");
        });
    </script>
    </form>
</body>
</html>

My Webmethod in WebMethod.aspx.cs is:

[System.Web.Services.WebMethod]
public static string HelloWorld(string username)
{
    return string.Format("Hello, {0}", username);
}

In Global.asax.cs is Routing activated

void Application_Start(object sender, EventArgs e)
{
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

In App_Start have regitred the routes

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
    }
}

解决方案

In your App_Start folder, within your RouteConfig....

Comment out the following line or change it's RedirectMode:

//settings.AutoRedirectMode = RedirectMode.Permanent;

这篇关于通话过程中的WebMethod身份验证失败从jquery.ajx与AspNet.FriendlyUrls和AspNet.Identity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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