简体   繁体   English

如何在.NET中将www-authenticate标头添加到HttpListenerResponse

[英]How to add www-authenticate header to HttpListenerResponse in .NET

I have a custom Web Stack with custom self-hosted server (no IIS, no WCF). 我有一个带有自定义自托管服务器的自定义Web堆栈(没有IIS,没有WCF)。 It uses HttpListener to get HttpListenerContext that is processed by pipeline of modules. 它使用HttpListener来获取由模块管道处理的HttpListenerContext So, I am working with HttpListenerRequest and HttpListenerRespose in processing modules. 因此,我正在处理模块中使用HttpListenerRequestHttpListenerRespose

Now I need to return 401 Unauthorized to the client in some specific cases. 现在,在某些特定情况下,我需要将401未经授权返回给客户端。 Response should also include WWW-Authenticate header with specific challenge. 响应还应包括具有特定质询的WWW-Authenticate标头。 So, I can just set StatusCode of the HttpListenerRespose = 401 and it works fine. 因此,我只需要设置HttpListenerRespose = 401的StatusCode即可。 But when I try to add Www-Authenticate header to the resonse - I get an exception: "The 'WWW-Authenticate' header must be modified using the appropriate property or method. Parameter name: name". 但是,当我尝试将Www-Authenticate标头添加到共鸣时-我得到一个例外:“必须使用适当的属性或方法来修改'WWW-Authenticate'标头。参数名称:name”。

As I can see, WWW-Authenticate header is protected for response. 如我所见,WWW-Authenticate标头受到保护以进行响应。 MSDN Documentation says that I can't "set a Content-Length, Keep-Alive, Transfer-Encoding, or WWW-Authenticate header using the Headers property". MSDN文档说,我不能“使用Headers属性设置Content-Length,Keep-Alive,Transfer-Encoding或WWW-Authenticate标头”。 There are KeepAlive and ContentLenght64 properties for two of them but "You cannot set the Transfer-Encoding or WWW-Authenticate headers manually" 其中两个具有KeepAlive和ContentLenght64属性,但是“您不能手动设置Transfer-Encoding或WWW-Authenticate标头”

So, what is a possible solution? 那么,什么是可能的解决方案? How should I return 401 response with specific WWW-Authenticate header correctly? 如何正确返回带有特定WWW-Authenticate标头的401响应?

HttpListenerResponse.AddHeader bypasses this restriction. HttpListenerResponse.AddHeader绕过此限制。 See https://referencesource.microsoft.com/#System/net/System/Net/HttpListenerResponse.cs,265 请参阅https://referencesource.microsoft.com/#System/net/System/Net/HttpListenerResponse.cs,265

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加 WWW-Authenticate header 会出现错误 - Adding the WWW-Authenticate header gives an error 如何解析来自 Www-Authenticate 的值 - How to parse values from Www-Authenticate Web API-如何在未经授权的请求中删除响应消息中的WWW-Authenticate标头 - Web API - How do I remove WWW-Authenticate header in response message with unauthorized request HttpResponseMessage,取消授权状态代码和内容//响应,无需WWW-Authenticate标头字段 - HttpResponseMessage with Unauthorize status code and Content // response without required WWW-Authenticate header field HttpListener与JWT和Basic auth:如何发送WWW-Authenticate? (自托管) - HttpListener with JWT and Basic auth: how to send WWW-Authenticate? (Self-Hosted) 微软欧文 | 在未经授权的情况下添加 WWW-Authenticate - Microsoft Owin | Adding WWW-Authenticate in case of unauthorized HttpListenerResponse添加不需要的Set-Cookie标头 - HttpListenerResponse adding unwanted Set-Cookie header 如何将原始 HTTP 响应解析为 HttpListenerResponse? - How to parse a raw HTTP response as HttpListenerResponse? 如何在.NET中验证LDAP - How to Authenticate LDAP in .NET 如何从 UnityWebRequest 中正确检索 HttpListenerResponse 的内容长度? - How to correctly retrieve content length of a HttpListenerResponse from UnityWebRequest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM