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

2

19

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);
    }
}
Andee answered 17/11, 2013 at 15:7 Comment(0)
W
55

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

Comment out the following line or change its RedirectMode:

//settings.AutoRedirectMode = RedirectMode.Permanent;
Willywilly answered 18/11, 2013 at 16:33 Comment(1)
Do you know why this fixes it?Kling
N
1

It is not exactly the same use case, but worth mentioning if you are using a web service and had the "Authentication failed" message. Please check that your web service has the attribute "[System.Web.Script.Services.ScriptService]". This allows the web service to be called from script.

Example:

[System.Web.Script.Services.ScriptService]
public class WS_Default : System.Web.Services.WebService
{
    ...
}
Narwhal answered 30/10, 2014 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.