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

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

问题描述

WebApi 在我的代码中抛出异常时遇到问题:

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;
                });
        }

线上发生异常:

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 路由.非常感谢任何帮助....

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

推荐答案

找到答案 此处 和示例处理程序 此处.

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天全站免登陆