【问题标题】:Aurelia auth - custom response message - console errorAurelia auth - 自定义响应消息 - 控制台错误
【发布时间】:2026-01-22 13:20:20
【问题描述】:

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

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 结果吗?

【问题讨论】:

    标签: c# .net authentication aurelia aurelia-auth


    【解决方案1】:

    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-authenticationaurelia-auth 的分支,它本身就是伟大的Satellizer library 的一个端口。有了新包,我现在可以做更多事情(刷新令牌、从服务器返回自定义消息等)..

    【讨论】:

      最近更新 更多