简体   繁体   English

使用C#在IIS7中以编程方式设置“连接为”用户

[英]Programmatically setting the 'Connect as' user in IIS7 using C#

I'm attempting to do this using the following code snippet, but the FindElement keeps giving me errors indicating it doesn't exist in the current context. 我正在尝试使用下面的代码片段执行此操作,但是FindElement不断显示错误,表明它在当前上下文中不存在。 Ultimately what I'm trying to do is set the username and password the website uses in the connect as area. 最终,我要尝试的是设置网站在“连接为”区域中使用的用户名和密码。 This is different from the impersonation user. 这与模拟用户不同。

using Microsoft.Web.Administration;

using Microsoft.Web.Management;

using Microsoft.Web.Media.TransformManager.Common;

using Microsoft.Web.Media.TransformManager;

using System.Web.Configuration;

using System.Collections;

                        Configuration config = iisManager.GetApplicationHostConfiguration();
                        ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
                        ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
                        ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"Default Web Site");
                        ConfigurationElementCollection applicationCollection = siteElement.GetCollection();
                        ConfigurationElement applicationElement = FindElement(applicationCollection, "application", "path", @"/MyNewVirtualDir");
                        ConfigurationElementCollection virtualDirCollection = applicationElement.GetCollection();
                        ConfigurationElement virtualDirElement = FindElement(virtualDirCollection, "virtualDirectory", "path", @"/");
                        virtualDirElement.Attributes["userName"].Value = "MYDOMAIN\\MyUser";
                        virtualDirElement.Attributes["password"].Value = "MyPassword";

EDIT : So as I was staring at the question after beating my head against this for a few days, I discovered you can accomplish this using ServerManager in the following context. 编辑:因此,当我对这个问题ating了几天后盯着这个问题时,我发现您可以在以下上下文中使用ServerManager来完成此任务。

ServerManager iisManager = new ServerManager()
                        site = iisManager.Sites.FirstOrDefault(a => a.Name.Contains("Default"));
                        site.VirtualDirectoryDefaults.Password = tbImpersonatorPassword.Text;
                        site.VirtualDirectoryDefaults.UserName = tbImpersonatorUser.Text;

So as I was staring at the question after beating my head against this for a few days, and apparently you can accomplish this using Servermanager in the following context. 因此,当我对这个问题this了几天之后一直盯着这个问题时,显然您可以在以下情况下使用Servermanager来完成此任务。

ServerManager iisManager = new ServerManager()
                        site = iisManager.Sites.FirstOrDefault(a => a.Name.Contains("Default"));
                        site.VirtualDirectoryDefaults.Password = tbImpersonatorPassword.Text;
                        site.VirtualDirectoryDefaults.UserName = tbImpersonatorUser.Text;

Setting the Username and Password on the VirtualDirectoryDefaults may not yield the results you are looking for. VirtualDirectoryDe​​faults上设置用户名和密码可能不会产生您想要的结果。 Instead you may want to locate the app within this Site object whose path is the root (hence the .Path.Equals("/") filter on the query), then modify that apps Virtual Directory username and password. 取而代之的是,您可能希望在此站点对象中定位路径为根的应用程序(因此查询中的.Path.Equals(“ /”)过滤器),然后修改该应用程序的虚拟目录用户名和密码。

This can be accomplished with the following method (Please Note: this method assumes that you have already located the desired Site via a search on the ServerManagers Sites collection and that you are passing that Site object into this method). 这可以通过以下方法来完成(请注意:该方法假定您已经通过ServerManagers Sites集合上的搜索找到了所需的Site,并且将该Site对象传递给了该方法)。 Be sure to dispose of the ServerManager object when you are done in order to avoid a memory leak. 完成操作后,请确保处置ServerManager对象,以避免内存泄漏。

    public static void SetConnectAsAccount(Site site, string username, string password)
    {
        if (site == null)
        {
            throw new ArgumentNullException("site");
        }

        if (string.IsNullOrWhiteSpace(username))
        {
            throw new ArgumentNullException("username");
        }

        if (string.IsNullOrWhiteSpace(password))
        {
            throw new ArgumentNullException("password");
        }

        foreach (var app in site.Applications.Where(c => c.Path.Equals("/")))
        {
            try
            {
                // set the Connect-As Accounts login credentials to the Service Acount
                var appVirDir = app.VirtualDirectories.Where(c => c.Path.Equals("/")).FirstOrDefault();

                if (appVirDir != null)
                {
                    appVirDir.UserName = username;
                    appVirDir.Password = password;
                }     
            }
            catch (Exception ex)
            {
                // log your exception somewhere so you know what went wrong
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM