内处理尚未使用的WebAPI委托处理程序分配 [英] The inner handler has not been assigned using WebApi Delegating handler

查看:214
本文介绍了内处理尚未使用的WebAPI委托处理程序分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,在的WebAPI我的code这儿抛出一个异常:

I have got a problem with WebApi throwing an exception here in my code:

public class WebApiAuthenticationHandler : DelegatingHandler
    {
        private const string AuthToken = "AUTH-TOKEN";

        protected override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {                  
            var requestAuthTokenList = GetRequestAuthTokens(request);
            if (ValidAuthorization(requestAuthTokenList))
            {
                // EXCEPTION is occuring here!....
                return base.SendAsync(request, cancellationToken);
            }

            /*
            ** This will make the whole API protected by the API token.
            ** To only protect parts of the API then mark controllers/methods
            ** with the Authorize attribute and always return this:
            **
            ** return base.SendAsync(request, cancellationToken);
            */
            return Task<HttpResponseMessage>.Factory.StartNew(
                () =>
                {
                    var resp = new HttpResponseMessage(HttpStatusCode.Unauthorized)
                    {
                        Content = new StringContent("Authorization failed")
                    };

                    //var resp = new HttpResponseMessage(HttpStatusCode.Unauthorized);                                                                                   
                    //resp.Headers.Add(SuppressFormsAuthenticationRedirectModule.SuppressFormsHeaderName,"true");
                    return resp;
                });
        }

唯一的例外是发生就行了:

The exception is happening on the line:

base.SendAsync(request, cancellationToken);

我还没有线索如何解决此问题。我在我的路由表如下:

I haven't a clue how to fix this. I have the following in my route table:

    routes.MapHttpRoute("NoAuthRequiredApi", "api/auth/", new { Controller = "Auth" });
    routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional }, null, new WebApiAuthenticationHandler());

这发生在这条路线是DefaultApi路线。任何帮助非常AP preciated ....

The route this happens on is the DefaultApi route. Any help greatly appreciated....

推荐答案

找到了答案这里和例如处理器<一个href=\"https://github.com/thinktecture/Thinktecture.IdentityModel.45/blob/master/IdentityModel/Thinktecture.IdentityModel/Tokens/Http/AuthenticationHandler.cs\">here.

Found the answer here and an example handler here.

您需要设置InnerHandler你想传递给这个请求。

You need to set the InnerHandler you want the request passed on to.

只需添加到您的构造函数:

Simply add this to your constructor:

public class WebApiAuthenticationHandler : DelegatingHandler
{
    public WebApiAuthenticationHandler(HttpConfiguration httpConfiguration)
    {
        InnerHandler = new HttpControllerDispatcher(httpConfiguration); 
    }

和传递给GlobalConfiguration参考创建一个新的实例时:

And pass in a reference to GlobalConfiguration when creating a new instance:

routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional }, null, WebApiAuthenticationHandler(GlobalConfiguration.Configuration));

这篇关于内处理尚未使用的WebAPI委托处理程序分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆