2

我有一个controllerwith authorize 属性,我可以在其中执行诸如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();
}

阿贾克斯

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;
}
4

0 回答 0