none
Issue while adding HTTP Response Header to a site hosted in azure web role running with more than one instance.

    Question

  • Hi ALL,

    We have a problem while trying to add HTTP Response Headers to add privacy policy to the site hosted in Azure web role.Following is the APPCMD command
    that we are trying to execute and it accepts the parameters as follows:

    %windir%\system32\inetsrv\APPCMD.EXE set config "SITENAME" /section:httpProtocol /+customHeaders.[name='P3P',value='policyref="http://www.microsoft.com/w3c/p3p.xml"']

    problem is that we have 2 instances of Azure and the SITENAME is generated dynamically during deployment for each of this VM instance.
    If we dont provide the site name, by default the privacy policy gets added to the root and it doesnt works.
    Is there any way to generalise this Appcmd command to take the SITENAME as the first site hosted?


    Thanks in advance for help.
    -Heena

    Wednesday, August 03, 2011 7:53 PM

Answers

  • Instead of writing admin code to configure IIS, you can directly write the setting in the web.config file:

    <configuration>
      <system.webServer>
        <httpProtocol>
          <customHeaders>
            <add name="p3p" value="policyref=&quot;http://www.microsoft.com/w3c/p3p.xml&quot;"/>
          </customHeaders>
        </httpProtocol>
      </system.webServer>
    </configuration>
    

    Thanks.


    Wenchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    • Marked as answer by HeenaP Friday, August 05, 2011 11:03 AM
    Thursday, August 04, 2011 2:51 AM

All replies

  • I believe the site doesn't actually exist until after startup tasks are run, so I think you'll need to do this from OnStart in your RoleEntryPoint. This code in WebRole.cs should do the trick, but you'll need to run the role elevated (using <Runtime executionEnvironment="elevated" /> in ServiceDefinition.csdef):

    public override bool OnStart()
    {
      using (ServerManager serverManager = new ServerManager())
      {
        Configuration config = serverManager.GetWebConfiguration(RoleEnvironment.CurrentRoleInstance.Id + "_Web");
        ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol");
        ConfigurationElementCollection customHeadersCollection = httpProtocolSection.GetCollection("customHeaders");
    
        ConfigurationElement addElement = customHeadersCollection.CreateElement("add");
        addElement["name"] = @"P3P";
        addElement["value"] = @"policyref=""http://www.microsoft.com/w3c/p3p.xml""";
        customHeadersCollection.Add(addElement);
    
        serverManager.CommitChanges();
      }
    
      return base.OnStart();
    }
    
    

    Thursday, August 04, 2011 12:08 AM
  • Instead of writing admin code to configure IIS, you can directly write the setting in the web.config file:

    <configuration>
      <system.webServer>
        <httpProtocol>
          <customHeaders>
            <add name="p3p" value="policyref=&quot;http://www.microsoft.com/w3c/p3p.xml&quot;"/>
          </customHeaders>
        </httpProtocol>
      </system.webServer>
    </configuration>
    

    Thanks.


    Wenchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    • Marked as answer by HeenaP Friday, August 05, 2011 11:03 AM
    Thursday, August 04, 2011 2:51 AM
  • That's so much better. :-)
    Thursday, August 04, 2011 5:37 AM
  • Thanks, It is so easy and straightforward :)
    Friday, August 05, 2011 11:03 AM
  • Hi,

    This question is unrelated to the above thread but here I am trying to read similar custom headers but getting its throwing an error -> Invalid index. (Exception from HRESULT: 0x80070585) on Line 5

    I have used the code below to read :

     

    ServerManager serverManager = new ServerManager(); //Line 1

     Configuration config = serverManager.GetWebConfiguration("Default Web Site");

    ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol");

    ConfigurationElementCollection customHeadersCollection = httpProtocolSection.GetCollection("customHeaders");

    string first_name = customHeadersCollection.GetAttributeValue("FIRST_NAME").ToString(); //Line 5

    Thanks!

    Monday, January 09, 2012 5:07 PM