簡體   English   中英

如何在iis7中以編程方式完成應用程序的url授權?

[英]How to programatically accomplish url authorization in iis7 for an application?

我創建了一個ftp站點“ TestFtpSite”和一個路徑為“ / LocalUser / demor”的應用程序。 這是ApplicationHost.config中的配置。

<site name="TestFtpSite" id="3">
            <application path="/" applicationPool="TestFtpPool">
                <virtualDirectory path="/" physicalPath="F:\empty-ftp-folder" />
            </application>
            <application path="/LocalUser/demor" applicationPool="TestFtpPool">
                <virtualDirectory path="/" physicalPath="F:\HJ_STORAGE\demor" />
            </application>
            <bindings>
                <binding protocol="ftp" bindingInformation="*:21:" />
            </bindings>
            <ftpServer>
                <security>
                    <ssl controlChannelPolicy="SslAllow" dataChannelPolicy="SslAllow" />
                    <authentication>
                        <basicAuthentication enabled="true" />
                    </authentication>
                </security>
                <userIsolation mode="IsolateAllDirectories">
                    <activeDirectory />
                </userIsolation>
            </ftpServer>
        </site>

在閱讀理解iis-url-authorization之后 ,我發現我們可以在ApplicationHost.config文件中添加位置標記以保護應用程序。 但是我找不到關於如何將帶有授權規則的位置標記實用地添加到配置文件的任何代碼段或api。
我想用C#務實地實現以下目標。

<location path="TestFtpsite/LocalUser/Bob"> 
    <system.ftpServer> 
        <security> 
            <authorization> 
                <clear />
                <add accessType="Allow" users="Bob" permissions="Read, Write"/>                  
            </authorization> 
        </security> 
    </system.ftpServer> 
</location> 

--------------更新----------------------

最后,我通過在Powershell中以編程方式解鎖IIS配置部分的啟發來解決了該問題

這是我的解決方案,希望對您有所幫助。

        // be sure to reference Microsoft.Web.Administration firstly
        ServerManager sm = new ServerManager();
        Configuration config= sm.GetApplicationHostConfiguration();

        /*************************
         * Unlock the section
         * ***********************/
        ConfigurationSection section = config.GetSection("system.ftpServer/security/authorization", "TestFtpSite/LocalUser/demor");
        section.OverrideMode = OverrideMode.Allow;
        sm.CommitChanges(); 

        // Get a new instance of the configuration object
        config = sm.GetApplicationHostConfiguration();
        section = config.GetSection("system.ftpServer/security/authorization", "TestFtpSite/LocalUser/demor");
        ConfigurationElementCollection authCollection = section.GetCollection();

        ConfigurationElement clearElement = authCollection.CreateElement("clear");
        authCollection.Add(clearElement);

        ConfigurationElement addElement =  authCollection.CreateElement("add");
        addElement.SetAttributeValue("accessType", "Allow");
        addElement.SetAttributeValue("users", "demor");
        addElement.SetAttributeValue("permissions", "Read, Write");
        authCollection.Add(addElement);

        sm.CommitChanges(); 

使用授權屬性。 用法取決於所使用的技術,但是對於MVC,您可以將其放在控制器或類似的動作上。

[Authorize(Users="Bob")]
public ActionResult LocalUser()
{
    . . .
}

暫無
暫無

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

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