.net – 在DelegatingHandler中设置WWW-Authenticate响应头会对w
发布时间:2020-12-16 03:40:36 所属栏目:asp.Net 来源:网络整理
导读:我通过发送会话令牌使用自定义身份验证机制.在DelegatingHandler中检查令牌的存在,DelegatingHandler根据情况设置当前主体.如果主体未被授权调用ApiController方法,则控制器发送401 Unauthorized状态.由于RFC 2616需要在发送401响应时始终设置WWW-Authentica
我通过发送会话令牌使用自定义身份验证机制.在DelegatingHandler中检查令牌的存在,DelegatingHandler根据情况设置当前主体.如果主体未被授权调用ApiController方法,则控制器发送401 Unauthorized状态.由于RFC 2616需要在发送401响应时始终设置WWW-Authenticate标头,因此我的DelegatingHandler负责处理此问题.
现在,在Web API自托管的情况下,应该响应的请求可以在Windows 7上正常运行,但在Windows Server 2003上,它会因“远程主机强行关闭现有连接”而死亡.此外,我注意到控制器方法中的断点在W2k3上被击中两次,而在Win7中被击中一次,好像HttpClient在收到401响应时以某种方式重试了请求. 当我使用WWW-Authenticate标头取消注释该行时,该程序正常工作.请参阅下面的代码示例,以获取控制台应用程序中的简约再现示例. TestController.cs: public class TestController : ApiController { public HttpResponseMessage Get() { return Request.CreateErrorResponse(HttpStatusCode.Unauthorized,"go away"); } } AuthenticationHandler.cs: public class AuthenticationHandler : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) { return base.SendAsync(request,cancellationToken).ContinueWith<HttpResponseMessage>(task => { HttpResponseMessage response = task.Result; if ( response.StatusCode == HttpStatusCode.Unauthorized && !response.Headers.Contains("WWW-Authenticate") ) { // comment out this line and the code works response.Headers.Add("WWW-Authenticate","SessionToken"); } return response; }); } } Program.cs中: static void Main(string[] args) { HttpSelfHostConfiguration config = new HttpSelfHostConfiguration("http://localhost:81"); config.Routes.MapHttpRoute( name: "DefaultApi",routeTemplate: "api/{controller}" ); config.MessageHandlers.Add(new AuthenticationHandler()); using ( HttpSelfHostServer server = new HttpSelfHostServer(config) ) using ( HttpClient client = new HttpClient() ) { server.OpenAsync().Wait(); try { HttpResponseMessage response = client.GetAsync("http://localhost:81/api/test").Result; Console.Out.WriteLine(response.StatusCode); } catch ( AggregateException ex ) { Console.Out.WriteLine(ex.ToString()); } server.CloseAsync().Wait(); } Console.In.ReadLine(); } 我是否正确调用了API?什么想法可能是错的? 解决方法
您需要安装.net Framework 4.0.3. .net框架的早期版本无法设置www-authenticate标头.得到它
here,关于这个问题的更多信息是在
KB2600211.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 排序 – sortdir没有在MVC 3 WebGird中使用枚举数据类型
- asp.net – Azure WebJob超时配置设置
- 在ASP.NET中动态加载css
- 是否可以在IIS 7中将虚拟目录设置为根目录?
- 如何从本地asp.net 4.0 Web应用程序发送支持图像的电子邮件
- 深入理解C# 3.x的新特性(2):Extension Method[上篇]
- 可以使用ASP.NET路由为.ashx(IHttpHander)处理程序创建“干
- asp.net – 在自己的AppPool中运行每个网站有哪些优点和/或
- asp-classic – 在经典的asp-reposting表单上捕获SQL注入会
- asp.net – 加快你的IIS
推荐文章
站长推荐
- Asp.Net,DropDownList,AutoPostBack和Google Chr
- asp.net-identity – 将SimpleMembership迁移到I
- 如何在asp.net中检测服务器端的浏览器关闭?
- 使用IIS/ASP.NET反向AJAX
- 在ASP.net c#web应用程序中显示警告框
- asp.net-mvc – 用于拆分日期时间字段的ASP.NET
- asp.net – Mono apache2 =服务暂时不可用(503)
- asp.net-mvc – 如何在视图中获取会话值asp.net核
- 具有自定义角色的ASP.NET MVC和Windows身份验证
- asp.net-mvc-3 – ASP.NET MVC 3.0 – 为什么在视
热点阅读