1

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;
1
  • 1
    Please add this edit as answer, it could be usefull for future readers Commented Jul 1, 2014 at 14:56

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.