获取“身份验证失败。 System.InvalidOperationException” jquery 调用 Webmethod 时出现间歇性错误
Getting “Authentication failed. System.InvalidOperationException” error intermittently with jquery call to Webmethod
我在 ASP.NET Webform 中有一个 WebMethod,位于 foo.aspx 页面的代码后面
[WebMethod]
public static String foo()
{
return "";
}
我使用 jQuery
调用它
$.ajax({
type: "POST",
url: '<%= ResolveUrl("foo.aspx/foo")%>',
data: JSON.stringify(dataToServer),
contentType: "application/json; charset=utf-8",
success: function (data ) {
// work with data here
}
});
这是来自服务器的响应头
Request
Request Method:POST
Status Code:401 Unauthorized
2:2
Cache-Control:private
Content-Length:105
Content-Type:application/json; charset=utf-8
Date:Tue, 20 Jan 2015 04:40:36 GMT
jsonerror:true
Server:Microsoft-IIS/7.5
X-Powered-By:ASP.NET
响应正文
{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}
背景资料
- 我不使用 ASP.NET 友好 URL 所以请不要建议我转
自动重定向模式 = 关闭。
我确实在我的站点中使用了 WebApi,但不确定是否
这是相关的。这是我的路由配置
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{name}"
);
RouteTable.Routes.MapHttpRoute(
name: "DefaultApiNoParameter",
routeTemplate: "api/{controller}/{action}"
);
我构建的站点作为子应用程序托管在另一个站点中
ASP.NET 申请。因此,让我们调用我的应用程序栏,它托管在 foobar 中,在域 foo.com 上,因此 http://foo.com/foobar/bar. Sometimes, after a number of refreshes, the browser pop up "The site http://foo.com:80 需要您输入密码等等......所有站点都启用了匿名身份验证和表单身份验证,其他身份验证已禁用。
这是我的web.config身份验证设置,想法是
应用程序是 public,只有 foo.aspx 是私有的和密码
受保护。
<authentication mode="Forms">
<forms name="cookie" loginUrl="login.aspx" defaultUrl="foo.aspx">
<credentials passwordFormat="SHA1">
<user name="foo"
password="xxxxxxxx"/>
<user name="bar"
password="xxxxxxxxx"/>
</credentials>
</forms>
</authentication>
该应用程序在测试环境中运行良好
单一网络服务器。仅当部署到网络场时才会出现此问题
发生了。
- 错误间歇性发生,有时请求有效,有时却无效。刷新几次页面后,我将自动注销。
The application works just fine in the testing environment with a single web server. Only when deployed to a web farm then this problem happens.
当 encryption/decryption 键在所有实例中不匹配时通常会发生这种情况
如果您使用 SHA1
来散列密码,您需要确保网站的所有实例共享相同的 machineKey
,以便散列算法可以正确散列密码。
您可以通过将相应的键添加到 web.config
文件来实现,例如...
<machineKey decryption="AES"
validation="SHA1"
decryptionKey="22B83C3D43B0BA0E2"
validationKey="1C059088E7E510C83650449D8D6567B2" />
如与 ...key
值相关的 machineKey
元素的 MSDN 文档中引用...
Specifies a manually assigned key. This value must be manually set to
a string of hexadecimal characters to ensure consistent configuration
across a Web farm. The key should be 16 hexadecimal characters in
length when using DES encryption and 48 hexadecimal characters in
length when using Triple DES (3DES) or AES encryption. If keys shorter
than the maximum length are used, the keys should be created by a
truly random means, such as by using the RNGCryptoServiceProvider
class. ASP.NET can use Triple DES only on computers on which 128-bit
encryption is available.
有关信息,请参阅 MSDN
我在 ASP.NET Webform 中有一个 WebMethod,位于 foo.aspx 页面的代码后面
[WebMethod]
public static String foo()
{
return "";
}
我使用 jQuery
调用它 $.ajax({
type: "POST",
url: '<%= ResolveUrl("foo.aspx/foo")%>',
data: JSON.stringify(dataToServer),
contentType: "application/json; charset=utf-8",
success: function (data ) {
// work with data here
}
});
这是来自服务器的响应头
Request
Request Method:POST
Status Code:401 Unauthorized
2:2
Cache-Control:private
Content-Length:105
Content-Type:application/json; charset=utf-8
Date:Tue, 20 Jan 2015 04:40:36 GMT
jsonerror:true
Server:Microsoft-IIS/7.5
X-Powered-By:ASP.NET
响应正文
{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}
背景资料
- 我不使用 ASP.NET 友好 URL 所以请不要建议我转 自动重定向模式 = 关闭。
我确实在我的站点中使用了 WebApi,但不确定是否 这是相关的。这是我的路由配置
RouteTable.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{name}" ); RouteTable.Routes.MapHttpRoute( name: "DefaultApiNoParameter", routeTemplate: "api/{controller}/{action}" );
我构建的站点作为子应用程序托管在另一个站点中 ASP.NET 申请。因此,让我们调用我的应用程序栏,它托管在 foobar 中,在域 foo.com 上,因此 http://foo.com/foobar/bar. Sometimes, after a number of refreshes, the browser pop up "The site http://foo.com:80 需要您输入密码等等......所有站点都启用了匿名身份验证和表单身份验证,其他身份验证已禁用。
这是我的web.config身份验证设置,想法是 应用程序是 public,只有 foo.aspx 是私有的和密码 受保护。
<authentication mode="Forms"> <forms name="cookie" loginUrl="login.aspx" defaultUrl="foo.aspx"> <credentials passwordFormat="SHA1"> <user name="foo" password="xxxxxxxx"/> <user name="bar" password="xxxxxxxxx"/> </credentials> </forms> </authentication>
该应用程序在测试环境中运行良好 单一网络服务器。仅当部署到网络场时才会出现此问题 发生了。
- 错误间歇性发生,有时请求有效,有时却无效。刷新几次页面后,我将自动注销。
The application works just fine in the testing environment with a single web server. Only when deployed to a web farm then this problem happens.
当 encryption/decryption 键在所有实例中不匹配时通常会发生这种情况
如果您使用 SHA1
来散列密码,您需要确保网站的所有实例共享相同的 machineKey
,以便散列算法可以正确散列密码。
您可以通过将相应的键添加到 web.config
文件来实现,例如...
<machineKey decryption="AES"
validation="SHA1"
decryptionKey="22B83C3D43B0BA0E2"
validationKey="1C059088E7E510C83650449D8D6567B2" />
如与 ...key
值相关的 machineKey
元素的 MSDN 文档中引用...
Specifies a manually assigned key. This value must be manually set to a string of hexadecimal characters to ensure consistent configuration across a Web farm. The key should be 16 hexadecimal characters in length when using DES encryption and 48 hexadecimal characters in length when using Triple DES (3DES) or AES encryption. If keys shorter than the maximum length are used, the keys should be created by a truly random means, such as by using the RNGCryptoServiceProvider class. ASP.NET can use Triple DES only on computers on which 128-bit encryption is available.
有关信息,请参阅 MSDN