使用WinNT://提供程序和目录服务连接远程计算机时忽略用户名/密码

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的模拟部分模拟服务帐户。我正在模拟的帐户在任何工作站上都没有管理员权限,因此我在计算机条目的构造函数中放入了用户名/密码,如下所示:

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

用户名是域帐户的用户名,该帐户在每台工作站上都具有本地管理员权限。如果我查看结果deComputer对象的Username属性,我可以看到用户名与我输入的用户名匹配。此外,如果我输入无效密码,它会抛出错误,因此它以某种方式进行身份验证。
然而,如果我现在尝试从远程工作站添加或删除用户,则会出现一般访问被拒绝的错误。如果我将ASP.Net使用的服务帐户作为本地管理员添加到该工作站,它将添加和删除没有问题。
所以接下来我尝试使用LogonAPI(advapi32.dll->LogonUser调用)登录到作为所有工作站本地管理员的用户帐户,模拟生成的WindowsIdentity并尝试仅运行原始的deComputer实例化。当我这样做时,除了Path之外的每个属性都返回OLE异常......
我对下一步要尝试什么感到困惑。任何帮助都将不胜感激。
--解决方法--
为了解决这个问题,我们创建了一个运行在本地管理员帐户下的Windows服务,因此没有任何问题运行代码。我们将所有更新推送到SQL数据库中的一个表中,服务会获取它们并处理它们。但是,我仍然非常想知道为什么这不起作用,并且直接从网站推送更新将是很好的。
3个回答

1

我尝试过AuthenticationTypes.Secure,但没有成功。我阅读了MSDN上的主要描述,它似乎并不像WNetAddConnection2和NetUseAdd适用于所有情况。此外,我该如何在DirectoryEntry中使用它们?我在MSDN文章中没有看到任何简单的方法,就像使用Logonuser时那样容易。 - Peter
函数LogonUser用于本地登录,但您需要进行远程登录。例如,如果您想通过HTTPS访问银行,则不会使用您的银行账户进行LogonUser调用,而是仅在目标计算机上进行远程登录。在这里也是一样的。如果目标计算机不信任源计算机或者您尝试使用仅存在于目标计算机上的用户帐户连接,则具有模拟权限的LogonUser将无法工作!您可以毫无问题地使用WNetAddConnection2NetUseAdd - Oleg
1
如果您使用WNetAddConnection2NetUseAdd与目标计算机建立连接,则它将用于对远程计算机的其他访问尝试。同时,DirectoryEntryWinNT提供程序也会使用它。要断开连接,可以使用WNetCancelConnection2NetUseDel。更多解释请参见http://stackoverflow.com/questions/3282927/manage-remote-service-using-alternate-credentials/3348967#3348967。我建议您尝试使用`WNetAddConnection2`和`WNetCancelConnection2`,您会发现它们正是您所需要的。 - Oleg
除了你之前帮我解决的问题,这个也起作用了。由于你没有提供任何代码,所以我已经在我的问题中发布了代码。 - Peter
为了在远程计算机上使用LogonUser,使用LOGON32_LOGON_NEW_CREDENTIALS。 - Bluebaron

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
    }
}

我已经尝试了上述方法。但是,即使我传递了正确的凭据,仍然会出现“访问被拒绝”的错误。 - cmm user

-1

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

我曾经遇到连接远程Windows时出现“错误(0x80004005):未指定的错误”的问题。 我解决了如下:

//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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接