mrsubwoof
mrsubwoof

Reputation: 193

Using Microsoft.Web.Administration Application to change authentication mode

I would like to change the authentication mode of a web site or application in IIS from Anonymous to Windows and Basic via C# code using Microsoft.Web.Administration. The executing code is not the code of the web application but an external tool. How do I do that?

Upvotes: 1

Views: 564

Answers (1)

Jalpa Panchal
Jalpa Panchal

Reputation: 12749

you could use below code to enable windows authentication by using c sharp code:

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample {

    private static void Main() {
        
        using(ServerManager serverManager = new ServerManager()) { 
            Configuration config = serverManager.GetApplicationHostConfiguration();
            
            ConfigurationSection windowsAuthenticationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", "htmlsite");
            windowsAuthenticationSection["enabled"] = true;
            
            serverManager.CommitChanges();
        }
    }
}

disable anonymous authentication:

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample {

    private static void Main() {
        
        using(ServerManager serverManager = new ServerManager()) { 
            Configuration config = serverManager.GetApplicationHostConfiguration();
            
            ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", "htmlsite");
            anonymousAuthenticationSection["enabled"] = false;
            
            serverManager.CommitChanges();
        }
    }
}

Upvotes: 2

Related Questions