我有一个controller
具有授权属性,其中我执行login
,logout
等内容。在使用javascript调用进行了多次登录操作后,所有其他操作仍然会返回401 Unauthorized
错误。在我的示例中,使用注销操作返回401并导致我的问题。
操作
[HttpGet]
[AllowAnonymous]
[Mvc.ValidateAntiForgeryToken]
public bool Login(string param)
{
var parameters = param.Split('/');
var username = parameters[0];
var password = parameters[1];
if (WebSecurity.Login(username, password, true))
return true;
return false;
}
[HttpGet]
[Mvc.ValidateAntiForgeryToken]
public void Logout()
{
WebSecurity.Logout();
}
的Ajax
public login(username: string, password: string): bool {
var url = this.baseUrl + "Account/Login/" +
encodeURIComponent(username) + "/" + encodeURIComponent(password);
var xhr: JQueryXHR = $.ajax({
type: "GET",
url: url,
dataType: "json",
contentType: "application/json; charset=utf-8",
async: false,
cache: false
});
if (xhr.status == 200)
if (xhr.responseText == "true")
return true;
return false;
}
public logout(): bool {
var url = this.baseUrl + "Account/Logout/";
var deferred: JQueryDeferred = $.Deferred();
var xhr = $.ajax({
type: "GET",
url: url,
dataType: "json",
contentType: "application/json; charset=utf-8",
cache: false,
async: false
});
if (xhr.status == 200)
return true;
return false;
}