【发布时间】:2016-09-27 08:21:41
【问题描述】:
我有一个 Angular 应用程序,它向服务器发送登录请求并返回我从服务器抛出的错误,以防用户名或密码无效或不正确。 无论如何,我在 $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);
}
【问题讨论】:
标签: c# angularjs exception-handling asp.net-web-api2