【发布时间】:2015-12-22 12:35:15
【问题描述】:
我跟着http://bitoftech.net/2014/12/15/secure-asp-net-web-api-using-api-key-authentication-hmac-authentication/做了一个自定义认证过滤器。
一切正常,但我无法让服务器在 401 上说任何话。它正确地给出了 www-authenicate 标头和状态代码 401,但没有内容/正文。
我尝试使用来自http://www.asp.net/web-api/overview/security/authentication-filters 的AuthenticationFailureResult,但没有帮助。我将AuthenticateAsync 转换为async 并忽略了await 警告。
这是我目前的工作,cmets 中的代码是我希望能做的,主要是让它使用任何格式化程序
//request.CreateResponse(HttpStatusCode.Unauthorized, new { Error = true, Message = "Token is invalid" });
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.Write("{ \"Error\" = true, \"Message\" = \"Token is invalid\" }");
context.ErrorResult = new UnauthorizedResult(new AuthenticationHeaderValue[0], request);
【问题讨论】:
标签: c# asp.net authentication asp.net-web-api2 asp.net-web-api