0
votes

I have written a simple custom Authorize attribute that is applied to several WebAPI methods. When I run in my localhost everything works as expected but when I move the binaries to a true web server I start getting the following error message:

"Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.Controllers.HttpActionContext.get_Request()'

If I remove my custom Authorize attribute everything works fine on both servers.

The full stack trace is:

{"Message":"An error has occurred.","ExceptionMessage":"Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.Controllers.HttpActionContext.get_Request()'.","ExceptionType":"System.MissingMethodException","StackTrace":" at MyApp.CustomAuthorizeAttribute.OnAuthorization(HttpActionContext actionContext) at System.Web.Http.Filters.AuthorizationFilterAttribute.OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.AuthorizationFilterAttribute.d__3.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()"}

The Custom Attribute (stripped down but behavior is the same) is:

 public class CustomAuthorizeAttribute : AuthorizeAttribute
{
    public override void OnAuthorization(HttpActionContext actionContext)
    {
        try
        {
            var authValue = actionContext.Request.Headers.Authorization;
            return;
        }
}
}

The problem seems to be centered on the HttpActionContext Request property. If I try to get the value from that property it throws the above error. If I remark out that line it runs fine.

Any ideas?

* Additional info * I am able to remove all code from the OnAuthorization method and simply return and it will throw the same error.

* More * The error happens when I try to access the Request method of HttpActionContext.

1
are you having version mismatch or doing something crazy in your build?Daniel A. White
Nothing crazy. But there may be some sort of version mismatch.John S
Have you taken a look at this question?StoriKnow
Check the solution in this link: stackoverflow.com/questions/46451247/… apicontroller-get-requestSeeSharepei
Check this link: stackoverflow.com/questions/46451247/… apicontroller-get-requestSeeSharepei

1 Answers

0
votes

This exception throw because some case your actionContext is null.

Change to

var authValue = actionContext != null ? actionContext.Request.Headers.Authorization : null;