我正在我的Web API 2应用程序中编写一些身份验证逻辑,并且我在主题行中收到错误。起初我只是返回Unauthorized(),但后来我在主题行中得到了错误。当我重新访问代码时,我注意到Intellisense没有为Unauthorized显示无参数构造函数,因此我不确定为什么不会触发构建错误。看起来contstructor只接受AuthenticationHeaderValues的集合。所以我更新了我的实现,只是为这个参数传递null,但我仍然在主题行中得到错误。
我没有得到编译器或运行时错误,说缺少或需要参数,只是找不到方法,这看起来很奇怪。知道问题可能是什么吗?你能提供一个我可以传递的参数的基本工作示例,或者这看起来是一个不同的问题吗?
更新
我按照下面的答案中的说明,但我仍然得到相同的错误。这是完整的堆栈跟踪:
"{\"Message\":\"An error has occurred.\",\"ExceptionMessage\":\"Method not found: 'System.Web.Http.Results.UnauthorizedResult System.Web.Http.ApiController.Unauthorized(System.Collections.Generic.IEnumerable`1<System.Net.Http.Headers.AuthenticationHeaderValue>)'.\",\"ExceptionType\":\"System.MissingMethodException\",\"StackTrace\":\" at SecurityApi.Controllers.AuthenticationController.<Authenticate>d__3.MoveNext()\\r\\n at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)\\r\\n at SecurityApi.Controllers.AuthenticationController.Authenticate(UserCredentials userCredentials)\\r\\n at lambda_method(Closure , Object , Object[] )\\r\\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass12.<GetExecutor>b__8(Object instance, Object[] methodParameters)\\r\\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\\r\\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()\"}"
答案 0 :(得分:0)
我试过它,它在我使用时起作用:
namespace TestWebApp.Api.Controllers
{
using System.Web.Http;
public class CapController : ApiController
{
public IHttpActionResult GetVehicle()
{
return this.Unauthorized();
}
}
}
它返回401 Unauthorized。
如果您想使用AuthenticationHeaderValues:
namespace TestWebApp.Api.Controllers
{
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Web.Http;
public class CapController : ApiController
{
public IHttpActionResult GetVehicle()
{
return this.Unauthorized(new List<AuthenticationHeaderValue>()
{
new AuthenticationHeaderValue("Authorization")
});
}
}
}
它会将此标题添加到响应中:
www-authenticate→授权,持票人