Подтвердить что ты не робот

Ошибка аутентификации при вызове 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="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>

Мой Web-метод в WebMethod.aspx.cs:

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

В Global.asax.cs активирована маршрутизация

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

В App_Start зарегистрировались маршруты

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

Ответ 1

В папке App_Start в вашем RouteConfig....

Прокомментируйте следующую строку или измените ее RedirectMode:

//settings.AutoRedirectMode = RedirectMode.Permanent;

Ответ 2

Это не совсем тот же вариант использования, но стоит упомянуть, если вы используете веб-службу и сообщение "Ошибка аутентификации". Убедитесь, что ваш веб-сервис имеет атрибут "[System.Web.Script.Services.ScriptService]". Это позволяет вызывать веб-службу из script.

Пример:

[System.Web.Script.Services.ScriptService]
public class WS_Default : System.Web.Services.WebService
{
    ...
}