asp.net – 没有使用WebApi委托处理程序分配内部处理程序
发布时间:2020-12-15 20:27:51 所属栏目:asp.Net 来源:网络整理
导读:我有一个WebApi在我的代码中抛出异常的问题: public class WebApiAuthenticationHandler : DelegatingHandler { private const string AuthToken = "AUTH-TOKEN"; protected override TaskHttpResponseMessage SendAsync( HttpRequestMessage request,Cance
我有一个WebApi在我的代码中抛出异常的问题:
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); 我不知道如何解决这个问题.我的路线表中有以下内容: routes.MapHttpRoute("NoAuthRequiredApi","api/auth/",new { Controller = "Auth" }); routes.MapHttpRoute("DefaultApi","api/{controller}/{id}",new { id = RouteParameter.Optional },null,new WebApiAuthenticationHandler()); 发生这种路由是DefaultApi路由.任何帮助非常感谢…. 解决方法
找到答案
here和一个示例处理程序
here.
您需要设置您希望传递的请求的InnerHandler. 只需将其添加到您的构造函数中即可: public class WebApiAuthenticationHandler : DelegatingHandler { public WebApiAuthenticationHandler(HttpConfiguration httpConfiguration) { InnerHandler = new HttpControllerDispatcher(httpConfiguration); } 并在创建新实例时传递对GlobalConfiguration的引用: routes.MapHttpRoute("DefaultApi",WebApiAuthenticationHandler(GlobalConfiguration.Configuration)); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – Orchard CMS Media中的文件大小上传限制
- ASP.NET Core - 源码解析 - Program.cs (一)
- asp.net-mvc – ValidationMessage仅适用于model和html属性
- ASP.NET GridView利用PagerTemplate自定义分页
- asp.net-mvc – 如何在ASP.NET MVC控制器中设置十进制分隔符
- ASP.NET API版本控制
- 如何让ASP.NET MVC为EF中的外键生成字段
- asp.net-mvc – ASP.NET MVC 4,迁移 – 如何在生产服务器上
- asp.net-mvc – ASP.Net MVC 4通用主要困难
- 使用NSSM把.Net Core部署至 Windows 服务
推荐文章
站长推荐
- asp-classic – 经典asp运行的用户是什么?
- asp.net – VS.NET 2015项目不会构建
- asp.net-mvc-2 – 如何在ASP.NET MVC2中枚举form
- asp-classic – 如何使用Microsoft.XMLHTTP指定代
- asp.net – System.Web.AspNetHostingPermission
- asp.net-mvc – 每个请求DbContext发生随机错误
- asp.net – 在后面的代码中无法识别嵌套的Repeat
- ASP.NET上的ASP.NET未来?
- asp.net – 如何加密web.config中的一个条目
- ASP.Net:尝试处理http:// localhost:5957 / h
热点阅读