已经坚持了几个小时
{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}
我试图在我的ASP.Net Webform中调用这个WebMethod
[WebMethod]
public static string GetClients(string searchTerm, int pageIndex)
{
string query = "[GetClients_Pager]";
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SearchTerm", searchTerm);
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
return GetData(cmd, pageIndex).GetXml();
}
从这个jquery.ajax
function GetClients(pageIndex) {
$.ajax({
type: "POST",
url: "ConsultaPedidos.aspx/GetClients",
data: '{searchTerm: "' + SearchTerm() + '", pageIndex: ' + pageIndex + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
}
但我总是得到这个错误:
POST
http://localhost:64365/ConsultaPedidos.aspx/GetClients
401 (未授权)
奇怪的是,在我开始验证用户
之前,这种情况一直有效<system.web>
...
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="/Dashboard" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
...
</system.web>
有什么想法吗?
答案 0 :(得分:138)
这让我发疯了。
在〜/ App_Start / RouteConfig.cs 内更改:
settings.AutoRedirectMode = RedirectMode.Permanent;
要:
settings.AutoRedirectMode = RedirectMode.Off;
(或只是评论该行)
如果启用了友好的URL,则需要更改
url: "ConsultaPedidos.aspx/GetClients",
要:
url: '<%= ResolveUrl("ConsultaPedidos.aspx/GetClients") %>',
希望这有助于其他人
答案 1 :(得分:11)
内部~/App_Start/RouteConfig.cs
更改
settings.AutoRedirectMode = RedirectMode.Permanent;
到
settings.AutoRedirectMode = RedirectMode.Off;
答案 2 :(得分:5)
401 Unauthorized意味着:
这证实了你所说的关于添加身份验证的内容,它显然也涵盖了这种方法。
因此,您是否希望访问此方法是否公开?
公开强>:
要允许访问公共资源(例如此webmethod),只需将其放在配置文件中的同一目录中:
<authorization>
<allow users="*" />
</authorization>
如果您将标记放在上面,那么它将为所有资源提供对所有类型用户的访问权限。因此,您可以添加以下标记来授予Web服务
<location path="YourWebServiceName.asmx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
<强>私人强>:
答案 3 :(得分:2)
不是专家,但是你试过把<allow users="*"/>
放在配置文件中吗?
您的请求应该使用GET方法而不是POST(用于创建)。
编辑:您似乎使用的是SOAP方法,无法从客户端调用,您应该使用RESFUL服务。
答案 4 :(得分:1)
我发现这个问题试图解决同样的问题,我解决的方式在这里看不到所以我发布给陷入同一瓶颈的人:
尝试使用您的方法使用WebMethod属性中的EnableSession = true
喜欢
[WebMethod(EnableSession = true)]
public static string MyMethod()
{
return "no me rindo hdp";
}
用这个解决了我的401错误。希望对你有所帮助。
答案 5 :(得分:1)
就我而言,我正在将WebMethod添加到窗体上的控件。需要将其添加到页面本身。
答案 6 :(得分:1)
您只需要在〜/ App_Start / RouteConfig.cs中注释掉
// settings.AutoRedirectMode = RedirectMode.Permanent;
(Or do this)
settings.AutoRedirectMode = RedirectMode.Off;
与Web.config文件中的身份验证无关。
答案 7 :(得分:0)
我遇到了同样的问题,并首先尝试了可用的解决方案并且确实有效。但是我意识到如果我创建一个新的web-service
并在App_Code
文件的web-service
文件夹中添加相关代码,那么我就不会收到任何错误。
答案 8 :(得分:0)
我知道你接受了你的回答。它实际上是在创建Web窗体应用程序时发生的,而不是将Authentication
更改为No Authentication
。
我们看到的默认身份验证为Authentication: Individual User Account
如果我们更改为Authentication: No Authentication
答案 9 :(得分:0)
我的网站使用的是ASP.NET表单身份验证,我通过反复试验得出结果,如果我在.asmx
页面和contentType: "application/x-www-form-urlencoded"
上调用了一个网络方法,我只能使其正常工作dataType: "xml"
答案 10 :(得分:0)
在我的情况下,问题出在调用Ajax.asmx的URL中,根据网络服务器设置,此URL不正确,例如"/qa/Handlers/AjaxLib.asmx/"
为我工作,而不是"/Handlers/AjaxLib.asmx/"
(在我的特定情况下,在PROD服务器上可以正常工作):
$.ajax({
url: '/qa/Handlers/AjaxLib.asmx/' + action,
type: "POST",
async: false,
data: data,
contentType: "application/json; charset=utf-8",
success: function () {
然后,我的AJAX被调用超出IIS虚拟应用程序目录“ qa”的范围,因此发生授权错误({"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}).
答案 11 :(得分:0)
使用ASP.NET AJAX允许从脚本中调用此Web服务。将以下属性添加到类中
[System.Web.Script.Services.ScriptService]
我遇到了类似的问题,添加此属性可以解决该问题。
答案 12 :(得分:0)
对我来说,我的JQuery Ajax调用中有一个无效的“ data:”参数。但是,而不是.NET或JQuery抱怨参数值,我(在我看来是误导性的)收到401未经授权错误。