简体   繁体   中英

WCF hosted as Windows service - Custom HTTP header is not reflecting on Response header

I have a standalone C# WCF service running as a Windows service. I have the requirement to add custom headers like "X-Xss-Protection" to all responses. I have tried with the following script in app.config file but this doesn't add any HTTP header to the response.

    <system.webServer>
            <httpProtocol>
                <customHeaders>
                    <remove name="X-Powered-By"/>
                    <remove name="X-AspNet-Version"/>
                    <remove name="HTTPServer"/>
                    <remove name="Microsoft-IIS"/>
                    <add name="Strick-Transport-Security" value="max-age=31536000;includeSubDomains"/>
                    <add name="Content-Security-Policy" value="script-src 'unsafe-eval' https://www.google.com 'self' 'unsafe-inline'"/>
                    <add name="X-Xss-Protection" value="1; mode=block"/>
                    <add name="Feature-Policy" value="geolocation 'none'"/>
                    <add name="Cache-Control" value="no-cache, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0, s-maxage=0"/>
                    <add name="Pragma" value="no-cache"/>
                    <add name="Expires" value="0"/>
                </customHeaders>
            </httpProtocol>
        </system.webServer>

The reason why you cannot successfully insert the header through the IDispatchMessageInspector.BeforeSendReply method is probably because you did not add the [CustomBehavior] tag above the interface.

Add the tag like this:

[CustomBehavior]
Public interface IService

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM