简体   繁体   中英

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. 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.

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.

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. 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.

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). Be sure to dispose of the ServerManager object when you are done in order to avoid a memory leak.

    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
            }
        }
    }

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