1

将此aurelia -auth 插件与 .NET Core webAPI 一起使用,我希望在用户尝试使用不正确的用户名和/或密码登录时返回自定义错误。

网络接口:

public HttpResponseMessage Login()
{
    if (passwordValid)
    {
        return Request.CreateResponse(new LoginResponseViewModel { Token = token });
    }
    else
    {
        return Request.CreateResponse(HttpStatusCode.Unauthorized, "Incorrect username or password!");
    }
}

脚本

logIn(email, password) {
    return this.auth.login(email, password)
        .then(response => {
            console.log("success logged " + response);
        })
        .catch(err => {
            console.log("login failure: " + err);
        });
}
  1. 如何在登录功能捕获错误时访问我未经授权的响应自定义消息“用户名或密码错误”?
  2. 似乎 aurelia-auth 登录功能必须接收一个httpStatusCode.Unauthorized才能使其了解密码/用户名不正确,但这会产生401 (Unauthorized)控制台错误。我可以抑制这个控制台错误或者返回一个 aurelia-auth 可以理解的 json 结果吗?
4

1 回答 1

0

1.如何访问自定义的未授权响应消息

服务器响应:

return Request.CreateResponse(HttpStatusCode.Unauthorized, "My message");

客户:

return this.auth.login(email, password)
    .then(response => {
        console.log("success logged " + response);
    })
    .catch(error => error.json().then(serverError =>
        console.log(serverError) //My message 
    ));

2.抑制控制台错误或返回aurelia-auth可以理解的json结果

在当前版本的 aurelia-auth 中不可能返回 aurelia-auth 可以理解的 json 结果。

我决定用aurelia-authentication替换我当前的 aurelia-auth包。

Aurelia-authentication是 aurelia-auth 的一个分支,aurelia-auth本身就是一个伟大的Satellizer 库的一个端口。有了新的包,我现在可以做更多的事情,(刷新令牌,从服务器返回自定义消息等)..

于 2017-03-02T14:24:49.887 回答