I want to add users for my FTP site using C# web api so, I have tried this link but when I am using these commands using Powershell then, these commands successfully creating a new user. Irrespective to this when I want to automate this using below code
string scriptText = @"[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.Web.Management')" + "\n" +
"[Microsoft.Web.Management.Server.ManagementAuthentication]::CreateUser('" + UserName + "','" + Password + "')" + "\n" +
"[Microsoft.Web.Management.Server.ManagementAuthorization]::Grant('" + UserName + "','" + FtpSiteName + "', $FALSE)";
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
string strCmdText = "C:/Windows/System32/inetsrv/appcmd.exe set config " +
FtpSiteName + " - section:system.ftpServer / security / authorization / +'[accessType='Allow',users='" + UserName + "',permissions='Read, Write']' / commit:apphost";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
then, I am receiving an error at Collection<PSObject> results = pipeline.Invoke();
which is
{Exception calling "CreateUser" with "2" argument(s): "Filename: \\?\C:\Program Files\IIS Express\config\administration.config
Error: The configuration section 'system.webServer/management/authentication' cannot be read because it is missing a section declaration
"} System.Management.Automation.ErrorRecord
According to my observation, my C# code is editing this file (C:\Program Files\IIS Express\config\administration.config) where it could not find a relavent section whereas IIS Manager requires changes in this file (C:\Windows\System32\inetsrv\config\administration.config) for creating new user.
If anything is unclear then, please let me know. Thanks.