【C#】使用ServerManager類配置IIS身份驗證設(shè)置
當(dāng)前位置:點(diǎn)晴教程→知識管理交流
→『 技術(shù)文檔交流 』
我正在使用ServerManager類(來自Microsoft.Web.Administration)在運(yùn)行IIS 7的服務(wù)器上創(chuàng)建應(yīng)用程序,我想配置應(yīng)用程序是否在應(yīng)用程序基礎(chǔ)上使用匿名身份驗證或 Windows身份驗證,因此我不能簡單地要求IT更改根站點(diǎn)上的設(shè)置,該應(yīng)用程序的內(nèi)容屬于第三方,因此我不允許更改應(yīng)用程序內(nèi)的web.config文件。
Application類沒有公開任何有用的屬性,但也許我可以使用ServerManager的GetApplicationHostConfiguration方法完成某些事情? 聽起來你希望改變網(wǎng)站的互聯(lián)網(wǎng)信息系統(tǒng)配置,如果這是正確的,這樣的事情應(yīng)該有效:
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetWebConfiguration("Contoso");
ConfigurationSection authorizationSection = config.GetSection("system.webServer/security/authorization");
ConfigurationElementCollection authorizationCollection = authorizationSection.GetCollection();
ConfigurationElement addElement = authorizationCollection.CreateElement("add");
addElement["accessType"] = @"Allow";
addElement["roles"] = @"administrators";
authorizationCollection.Add(addElement);
serverManager.CommitChanges();
} 上面的代碼允許您創(chuàng)建一個授權(quán)規(guī)則,允許組中的特定用戶訪問特定站點(diǎn)。在這種情況下,該網(wǎng)站是Contoso。 然后,這將禁用該站點(diǎn)的匿名身份驗證,然后啟用Basic&該站點(diǎn)的Windows身份驗證: using(ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", "Contoso");
anonymousAuthenticationSection["enabled"] = false;
ConfigurationSection basicAuthenticationSection = config.GetSection("system.webServer/security/authentication/basicAuthentication", "Contoso");
basicAuthenticationSection["enabled"] = true;
ConfigurationSection windowsAuthenticationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", "Contoso");
windowsAuthenticationSection["enabled"] = true;
serverManager.CommitChanges();
} 或者,您可以根據(jù)需要添加IIS管理器用戶帳戶,您可以設(shè)置為某些權(quán)限來操縱和管理其他應(yīng)用程序。 using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetAdministrationConfiguration();
ConfigurationSection authenticationSection = config.GetSection("system.webServer/management/authentication");
ConfigurationElementCollection credentialsCollection = authenticationSection.GetCollection("credentials");
ConfigurationElement addElement = credentialsCollection.CreateElement("add");
addElement["name"] = @"ContosoUser";
addElement["password"] = @"P@ssw0rd";
addElement["enabled"] = true;
credentialsCollection.Add(addElement);
serverManager.CommitChanges();
} 互聯(lián)網(wǎng)信息系統(tǒng)具有很大的靈活性,它非常強(qiáng)大,通過那里參考的文件也非常深入。這些示例很難適應(yīng)您的特定用途,或者至少提供一定程度的理解,以使其按照您的意愿行事。 希望有幫助,這些例子來自here 該文章在 2021/5/11 15:46:34 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |