3

我正在使用 DirectoryServices 和 WinNT:// 提供程序连接到远程计算机。然后我检查一些组成员信息,并可能在指定的本地组中添加或删除域用户。

我已经能够使用 vb.net 控制台应用程序以及与我的本地盒子或我登录的帐户具有管理权限的任何盒子进行通信时,让所有这些代码顺利运行。

代码:

    string strUserPath = "WinNT://DomainName/someuser,user";
    DirectoryEntry deComputer = new DirectoryEntry("WinNT://" + Computername + ",computer");
    deComputer.RefreshCache();
    DirectoryEntry deGroup = deComputer.Children.Find("administrators", "group");

    IEnumerable members = deGroup.Invoke("members", null);
    List<DirectoryEntry> r = new List<DirectoryEntry>();

    foreach (object o in members)
    {
        DirectoryEntry deMember = new DirectoryEntry(o);

        r.Add(deMember);
    }

    deGroup.Invoke("Add", strUserPath);
    deGroup.CommitChanges();

    deGroup.Invoke("Remove", strUserPath);
    deGroup.CommitChanges();

因此,我将代码移至 ASP.Net Web 应用程序,该应用程序通过 web.config 的 Impersonate 部分模拟服务帐户。我模拟的帐户在任何工作站上都​​没有管理员权限,因此我将用户名/密码输入到计算机条目的构造函数中,如下所示:

DirectoryEntry deComputer = new DirectoryEntry("WinNT://" + Computername + ",computer", username, password);

用户名是在每个工作站上都​​具有本地管理员权限的域帐户的用户名。如果我查看生成的 deComputer 对象的 Username 属性,我可以看到用户名与我输入的匹配。此外,如果我输入了无效的密码,它会引发错误,因此它会以某种方式进行身份验证。

但是,如果我现在尝试从远程工作站添加或删除用户,则会收到一般访问被拒绝错误。如果我在该工作站上添加 ASP.Net 用作本地管理员的服务帐户,它将添加和删除没有问题。

因此,接下来我尝试使用 LogonAPI(advapi32.dll ->LogonUser 调用)以作为所有工作站上的本地管理员的用户帐户登录,模拟生成的 WindowsIdentitiy 并尝试仅运行原始的 deComputer 实例。当我这样做时,每个属性,excepty Path,都会返回一个 OLE 异常......

我对下一步要尝试什么感到很迷茫。任何帮助将不胜感激。

--解决方法--

为了解决这个问题,我们创建了一个在本地管理员帐户下运行的 Windows 服务,因此运行代码没有任何问题。我们将所有更新推送到 SQL 数据库中的表中,服务会提取并处理它们。但是,我仍然很想知道为什么这不起作用,并且直接从网站推送更新会很好。

4

3 回答 3

1

您是否尝试在用户名和密码之后AuthenticationTypes.Secure用作附加参数?DirectoryEntry

顺便说一句,如果你想连接到远程计算机,你不应该使用LogonUser. 正确的 API 是WNetAddConnection2(参见http://msdn.microsoft.com/en-us/library/aa385413.aspx)或NetUseAdd(参见http://msdn.microsoft.com/en-us/library/aa370645.aspx

于 2010-07-27T23:58:50.767 回答
0

由于这是一个热门问题,我将答案分离出来并将代码转换为 C#

这是对我有用的最终代码。这首先使用 WNetAddConnection2 建立连接,然后再使用 DirectoryEntry。

public static class CredentialSetter
{
    public static void SetCredentials()
    {
        string Computername = "SomeComputer";
        //Create connection to remote computer'
        using (NetworkConnection nc = new NetworkConnection("\\\\" + Computername + "", new NetworkCredential("Domain\\Login", "Password")))
        {
            //try connecting using DirectoryEntry to the same machine and add me as a user'
            string strUserPath = string.Format("WinNT://{0}/{1},user", "DOMAIN", "USER");
            DirectoryEntry deGroup = new DirectoryEntry("WinNT://" + Computername + "/Administrators");
            deGroup.RefreshCache();

            //add and remove the user from the group'
            deGroup.Invoke("Add", strUserPath);
            deGroup.CommitChanges();
            Console.WriteLine("User Added to computer " + Computername);

            deGroup.Invoke("Remove", strUserPath);
            deGroup.CommitChanges();
            Console.WriteLine("User Removed from computer " + Computername);

            deGroup.Close();
        }
        Console.ReadLine();
    }

    public class NetworkConnection : IDisposable
    {
        private string _networkName;
        public NetworkConnection(string networkName, NetworkCredential credentials)
        {
            _networkName = networkName;

            dynamic netResource = new NetResource
            {
                Scope = ResourceScope.GlobalNetwork,
                ResourceType = ResourceType.Disk,
                DisplayType = ResourceDisplaytype.Share,
                RemoteName = networkName
            };

            dynamic result = WNetAddConnection2(netResource, credentials.Password, credentials.UserName, 0);

            if (result != 0)
            {
                throw new IOException("Error connecting to remote share", result);
            }
        }

        ~NetworkConnection()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected void Dispose(bool disposing)
        {
            WNetCancelConnection2(_networkName, 0, true);
        }

        [DllImport("mpr.dll")]
        private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);

        [DllImport("mpr.dll")]
        private static extern int WNetCancelConnection2(string name, int flags, bool force);
    }

    [StructLayout(LayoutKind.Sequential)]
    public class NetResource
    {
        public ResourceScope Scope;
        public ResourceType ResourceType;
        public ResourceDisplaytype DisplayType;
        public int Usage;
        public string LocalName;
        public string RemoteName;
        public string Comment;
        public string Provider;
    }

    public enum ResourceScope : int
    {
        Connected = 1,
        GlobalNetwork,
        Remembered,
        Recent,
        Context
    }

    public enum ResourceType : int
    {
        Any = 0,
        Disk = 1,
        Print = 2,
        Reserved = 8
    }

    public enum ResourceDisplaytype : int
    {
        Generic = 0x0,
        Domain = 0x1,
        Server = 0x2,
        Share = 0x3,
        File = 0x4,
        Group = 0x5,
        Network = 0x6,
        Root = 0x7,
        Shareadmin = 0x8,
        Directory = 0x9,
        Tree = 0xa,
        Ndscontainer = 0xb
    }
}
于 2013-07-01T13:28:39.547 回答
-1

错误(0x80004005):未指定的错误

我在连接到远程窗口时遇到了一些问题,错误为 Error (0x80004005): Unspecified error。我解决如下:

//Define path
//This path uses the full path of user authentication
String path = string.Format("WinNT://{0}/{1},user", server_address, username);
DirectoryEntry deBase = null;
try
{
    //Try to connect with secure connection
    deBase = new DirectoryEntry(path, username, _passwd, AuthenticationTypes.Secure);

    //Connection test
    //After test define the deBase with the parent of user (root container)
    object nativeObject = deBase.NativeObject;
    deBase = deBase.Parent;

}
catch (Exception ex)
{
    //If an error occurred try without Secure Connection
    try
    {
        deBase = new DirectoryEntry(path, username, _passwd);

        //Connection test
        //After test define the deBase with the parent of user (root container)
        object nativeObject = deBase.NativeObject;
        deBase = deBase.Parent;
        nativeObject = deBase.NativeObject;

    }
    catch (Exception ex2)
    {
        //If an error occurred throw the error
        throw ex2;
    }
}

希望有帮助。Helvio Junior www.helviojunior.com.br

于 2014-07-12T00:35:57.900 回答