Authentication failed during call webmethod from jquery.ajx with AspNet.FriendlyUrls and AspNet.Identity

user3001838 picture user3001838 · Nov 17, 2013 · Viewed 17.6k times · Source

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

Answer

Sam picture Sam · Nov 18, 2013

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

Comment out the following line or change its RedirectMode:

//settings.AutoRedirectMode = RedirectMode.Permanent;