找不到方法:System.Web.Http.Results.UnauthorizedResult



我正在我的 Web API 2 应用程序中编写一些身份验证逻辑,但在主题行中收到错误。 起初我只是返回 Unauthorized(),但后来我在主题行中出现了错误。当我重新访问代码时,我注意到智能感知没有显示未授权的无参数构造函数,所以我不确定为什么这没有触发构建错误。看起来结构体只接受 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()"}"

我试过了,当我使用时它可以工作:

namespace TestWebApp.Api.Controllers
{
using System.Web.Http;
public class CapController : ApiController
{
public IHttpActionResult GetVehicle()
{
return this.Unauthorized();
}
}
}

它返回 401 未授权。

如果要使用 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 →授权,持有者

相关内容

  • 没有找到相关文章

最新更新