简体   繁体   中英

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?

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();
        }
    }
}

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