/// <summary> /// </summary> /// <param name="username"> </param> /// <param name="fqdn"> </param> /// <returns> </returns> public bool AddSite(string username, string fqdn) { try { var server = new IisServer(); // Check if binding allready exists. If it does, return false. if (server.BindingExists(fqdn)) return false; // Adding a new binding/host. var poolName = server.GetApplicationPoolName(username); // Create the site's root directory. var homeDirectory = Path.Combine(AppSettings.HomeDirectory, username, server.ReverseFqdn(fqdn)); var mkDir = MkDir(username, homeDirectory); var addHost = server.AddHost(username, fqdn, poolName, homeDirectory, "http", string.Format("*:80:{0}", fqdn)); return mkDir & addHost; } catch (Exception e) { Console.WriteLine(e); } return false; }
/// <summary> /// </summary> /// <param name="username"> </param> /// <param name="fqdn"> </param> /// <returns> </returns> public bool AddHost(string username, string fqdn) { try { var server = new IisServer(); // Check if binding allready exists. If it does, return false. if (server.BindingExists(fqdn)) return false; // Adding a new binding/host. var addBinding = server.AddBinding(username, fqdn); return addBinding; } catch (Exception e) { Console.WriteLine(e); } return false; }
/// <summary> /// </summary> /// <param name="username"> </param> /// <param name="passwd"> </param> /// <param name="fqdn"> </param> /// <returns> </returns> public bool AddUser(string username, string passwd, string fqdn) { try { var server = new IisServer(); // Check if binding allready exists. If it does, return false. if(server.BindingExists(fqdn)) return false; // Retrieving context from local machine. var context = new PrincipalContext(ContextType.Machine); // Check if user allready exists. Return false if it does. var user = UserPrincipal.FindByIdentity(context, username); if (user != null) return false; // Create user an set some appropriate values. user = new UserPrincipal(context) { Name = username, UserCannotChangePassword = false, PasswordNeverExpires = true, }; user.SetPassword(passwd); // Save the newly created user. user.Save(); // Adding new user to the IIS_IUSRS group. Maybe we should add a separate group for each user. Maybe in some other release... var grp = GroupPrincipal.FindByIdentity(context, "IIS_IUSRS"); if (grp != null) { grp.Members.Add(user); grp.Save(); } AddUserRemote(username, passwd); // Create the user's root directory. var rootHomeDirectory = Path.Combine(AppSettings.HomeDirectory, username); var mkDir = MkDir(username, rootHomeDirectory); // Adding a new application pool. var addApplicationPool = server.AddApplicationPool(username, passwd, fqdn); // Adding a new web site. var addSite = AddSite(username, fqdn); return mkDir & addApplicationPool & addSite; } catch (Exception ex) { // Here we could catch different exceptions, and maybe return a more sensable error response. Console.WriteLine("An error occured while creating the new user {0}. Exception:{1}", username, ex.Message); return false; } }