【发布时间】:2015-01-20 06:04:13
【问题描述】:
我在 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。有时,在多次刷新后,浏览器会弹出“该站点http://foo.com:80 需要您输入密码等等等等。”。所有站点都启用了匿名身份验证和表单身份验证,其他身份验证被禁用。
-
这是我的 web.config 身份验证设置,想法是 应用程序是公开的,只有 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> 该应用程序在测试环境中运行良好,具有 单一的网络服务器。只有当部署到网络场时才会出现这个问题 发生。
- 错误间歇性发生,有时请求有效,有时无效。刷新几次页面后,我会自动退出。
【问题讨论】:
标签: c# jquery asp.net forms-authentication asp.net-web-api2