[英]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.
因此,我正在处理模块中使用HttpListenerRequest和HttpListenerRespose 。
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.