簡體   English   中英

使用 Microsoft.Web.Administration 應用程序更改身份驗證模式

[英]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. 執行代碼不是 web 應用程序的代碼,而是外部工具。 我怎么做?

您可以使用以下代碼啟用 windows 身份驗證,方法是使用 c 清晰代碼:

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

禁用匿名身份驗證:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM