【问题标题】:Authentication failed - System.InvalidOperationException身份验证失败 - System.InvalidOperationException
【发布时间】:2017-03-26 17:55:50
【问题描述】:

我正在尝试使用main.js 文件中的以下代码发出简单的 Ajax 请求:

$.ajax({
    type: "POST",
    url: "SaveStyles.aspx/GetDate",
    data: { someParameter: "some value" },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        console.log(msg);
    }
});

SaveStyles.aspx.cs的C#中我有方法:

[WebMethod]
public static string GetDate(string someParameter)
{
    return "Sup!";
}

但在 JS 控制台中,我看到错误提示身份验证失败,System.InvalidOperationException。我不知道为什么会这样。 Mabye 我应该添加类似anti-csrf 令牌的东西吗?

【问题讨论】:

    标签: c# asp.net asp.net-mvc exception


    【解决方案1】:

    通过在 App_Start/RouteConfig.cs 中将 AutoDirectMode 设置为 Off 解决

    settings.AutoRedirectMode = RedirectMode.Off;
    

    并将 ScriptManager 添加到 EnablePageMethods 设置为“true”的 aspx 页面:

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
        </asp:ScriptManager>
    

    【讨论】:

    • 天哪。我不明白为什么要更改 App_Start AutoRedirectMode。我刚刚做了,我的问题已经解决了。非常感谢您提供此解决方案。
    猜你喜欢
    • 2010-12-25
    • 2014-01-24
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2014-05-23
    相关资源
    最近更新 更多