Hello, Welcome to my blog. If you like feel free to refer others

Tuesday, 13 May 2014

Programatically setting up Physical Path Credential on IIS server for a virtual directoty

Below mentioned function will do the same for you. But only thing you need to do add the reference of Microsoft.Web.Administration dll on your project(

private void SetPhysicalPathCredential(string virDirName)
                if (!String.IsNullOrEmpty(physicalPathCredentialUserDomain) && !String.IsNullOrEmpty(physicalPathCredentialUserUserName) && !String.IsNullOrEmpty(physicalPathCredentialUserPassword))
                    using (ServerManager manager = new ServerManager())
                        Site defaultSite = manager.Sites["Default Web Site"];

                        // Read the data back from the updated configuration system, 
                        // then modify the Physical Path credential.
                        Application apps = manager.Sites["Default Web Site"].Applications["/" + virDirName];
                        VirtualDirectory appDir = apps.VirtualDirectories[0];
                        LogMessage("Setting up Physical Path Credential: " + virDirName);
                        appDir["userName"] = physicalPathCredentialUserDomain + "\\" + physicalPathCredentialUserUserName;
                        appDir["password"] = physicalPathCredentialUserPassword;
                        LogMessage("Successfully set up Physical Path Credential");
            catch (Exception e)
                LogMessage("ERROR: Error Setting up Physical Path Credential " + virDirName);
                throw e;

No comments:

Post a Comment