如何设置虚拟目录凭据

时间:2014-09-05 09:48:32

标签: c# .net iis-7.5

我想为我的VirtualDirectory设置凭据。我以前创建过用户,我的工作是:

        DirectoryEntry site = new DirectoryEntry"IIS://localhost/W3SVC/1/Root");
        string className = site.SchemaClassName.ToString();
        if ((className.EndsWith("Server")) || (className.EndsWith("VirtualDir")))
        {
            DirectoryEntries vdirs = site.Children;
            DirectoryEntry existingDirectoryEntry = vdirs.OfType<DirectoryEntry>().SingleOrDefault(d => d.Name == name);
            if (existingDirectoryEntry != null)
                throw new Exception("The virtual directory you want to create already exists");

            DirectoryEntry newVDir = vdirs.Add(name, (className.Replace("Service", "VirtualDir")));
            newVDir.Username = username;
            newVDir.Password = password;
            newVDir.Properties["Path"][0] = path;
            newVDir.Properties["AccessScript"][0] = true;
            if (authFlags.HasValue)
                newVDir.Properties["AuthFlags"].Value = authFlags.Value;

            newVDir.CommitChanges();
        }

未设置用户名和密码目录已正确创建。当我设置用户名和密码时,我收到系统找不到指定路径的消息,但路径存在。也许我应该以某种方式改变认证类型?

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。在这种情况下,用户名和密码应该通过属性设置,而不是直接在对象中设置:

newVDir.Properties["UNCUserName"][0] = username;
newVDir.Properties["UNCPassword"][0] = password;

这解决了我的问题。