.net 和 jquery 突然给我 401 错误
.net and jquery all of a sudden giving me 401 err
这是我的 ajax 电话:
$J.ajax({
type: 'POST',
url: "/ajax/getCharts",
data: JSON.stringify(cids),
dataType: 'json',
asynch: false
})
这一直有效……直到昨天
我们正在使用 Microsoft.AspNet.Identity;
昨天我添加了更多用户角色,扩展了默认设置,例如 "admin"、"user" 等
现在我在 ajax.fail header:
中得到了这个错误
X-Responded-JSON:
{"status":401,"headers":{"location":"http://localhost:56970/Account/Login?ReturnUrl=%2Fajax%2FgetCharts"}}
哪种方式有意义 - 如果调用未通过身份验证
注意:
在我的 ajax 控制器中,我之前曾这样注释它(有效):
[Authorize(Roles = "User")]
public class ajaxController : BaseController
现在我扩展了它,包括我的新自定义用户角色,这样:
[Authorize(Roles = "Superuser, Poweruser, User")]
public class ajaxController : BaseController
那没用
我还在 RegisterRoutes 中添加了这个:
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Off;
routes.EnableFriendlyUrls(settings);
根据以下建议:
ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"
但是没用
这个页面对我来说毫无意义:
我在这里错过了什么?
我能理解的问题是当用户尝试登录时说我刚刚发送了登录请求..此时我没有登录意味着我目前没有任何角色并且你已经设置了授权过滤器ajax 允许某些角色的控制器
现在要解决此问题,请将 allowannonymus 属性仅用于 ajax 控制器中的登录操作
喜欢
[AllowAnonymous]
public async Task<ActionResult> Login()
{
}
p.s :- 不要忘记把它放在 post 操作中,如果你有一个
这是我的 ajax 电话:
$J.ajax({
type: 'POST',
url: "/ajax/getCharts",
data: JSON.stringify(cids),
dataType: 'json',
asynch: false
})
这一直有效……直到昨天
我们正在使用 Microsoft.AspNet.Identity;
昨天我添加了更多用户角色,扩展了默认设置,例如 "admin"、"user" 等
现在我在 ajax.fail header:
中得到了这个错误X-Responded-JSON: {"status":401,"headers":{"location":"http://localhost:56970/Account/Login?ReturnUrl=%2Fajax%2FgetCharts"}}
哪种方式有意义 - 如果调用未通过身份验证
注意:
在我的 ajax 控制器中,我之前曾这样注释它(有效):
[Authorize(Roles = "User")]
public class ajaxController : BaseController
现在我扩展了它,包括我的新自定义用户角色,这样:
[Authorize(Roles = "Superuser, Poweruser, User")]
public class ajaxController : BaseController
那没用
我还在 RegisterRoutes 中添加了这个:
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Off;
routes.EnableFriendlyUrls(settings);
根据以下建议:
ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"
但是没用
这个页面对我来说毫无意义:
我在这里错过了什么?
我能理解的问题是当用户尝试登录时说我刚刚发送了登录请求..此时我没有登录意味着我目前没有任何角色并且你已经设置了授权过滤器ajax 允许某些角色的控制器
现在要解决此问题,请将 allowannonymus 属性仅用于 ajax 控制器中的登录操作
喜欢
[AllowAnonymous]
public async Task<ActionResult> Login()
{
}
p.s :- 不要忘记把它放在 post 操作中,如果你有一个