我有一个角度的应用程序发送一个登录请求到服务器,并获取我从服务器发出的错误,以防万一用户名或密码无效或不正确。 无论如何,我在$ http服务上有一个获取错误消息的函数,但由于某种原因,错误消息以HTML标记的长字符串形式出现,并且我抛出的异常处于该HTML内的某处。我希望能够获得未经定制的原始消息或任何您称之为服务器或浏览器的原始消息。如何获得我从服务器角度抛出的错误消息?
我会感谢您的任何帮助。
更新:
我的错误是,我把我的例外情况的服务器上,而不是返回错误的。现在我修复了它,并且我返回了错误,但是我想返回一个状态码或一个正确的'http响应消息'。 SetError方法不允许我这样做。有没有其他方法可以做到这一点?
这里是我的方法:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
using (AuthRepository _repo = new AuthRepository())
{
bool user = _repo.Authenticate(context.UserName, context.Password);
if (user == false)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("sub", context.UserName));
identity.AddClaim(new Claim("role", "user"));
context.Validated(identity);
}