C# 出,我可以发布完整的工作来源星期一。。。祝你好运是的,如果你能发布完整的工作代码,那就太好了。谢谢你抽出时间!只要它能验证我的应用程序,并赋予它管理员权限来解锁用户,这就行了。让我把它说得更清楚些。首先,非常感谢大家!我将此限制为只搜索特定的OU,因为OU

C# 出,我可以发布完整的工作来源星期一。。。祝你好运是的,如果你能发布完整的工作代码,那就太好了。谢谢你抽出时间!只要它能验证我的应用程序,并赋予它管理员权限来解锁用户,这就行了。让我把它说得更清楚些。首先,非常感谢大家!我将此限制为只搜索特定的OU,因为OU,c#,windows-services,C#,Windows Services,出,我可以发布完整的工作来源星期一。。。祝你好运是的,如果你能发布完整的工作代码,那就太好了。谢谢你抽出时间!只要它能验证我的应用程序,并赋予它管理员权限来解锁用户,这就行了。让我把它说得更清楚些。首先,非常感谢大家!我将此限制为只搜索特定的OU,因为OU是一个通用帐户,就像10个人用来登录工厂周围的5个控制台一样,他们有时会搞砸,它会锁定该通用帐户,因此安全性我真的不担心,因为我已经覆盖了大部分基础,并且我的应用程序中存储的帐户将受到非常严格的监控close和creds改变了很多。lol只是为


出,我可以发布完整的工作来源星期一。。。祝你好运是的,如果你能发布完整的工作代码,那就太好了。谢谢你抽出时间!只要它能验证我的应用程序,并赋予它管理员权限来解锁用户,这就行了。让我把它说得更清楚些。首先,非常感谢大家!我将此限制为只搜索特定的OU,因为OU是一个通用帐户,就像10个人用来登录工厂周围的5个控制台一样,他们有时会搞砸,它会锁定该通用帐户,因此安全性我真的不担心,因为我已经覆盖了大部分基础,并且我的应用程序中存储的帐户将受到非常严格的监控close和creds改变了很多。lol只是为了避免这个简单任务的凌晨3点调用,因为某些原因,我的程序只会解锁我最近为测试创建的用户。在那里的用户如果我锁定其中一个,然后运行我的应用程序,它将在组合框中列出他们,我单击他们,然后单击解锁按钮。在调试和观察它通过我提供的代码后,它不工作。测试我创建的帐户是否找到,并将信息发送回结果,但其他用户没有结果保持空白可能是什么问题?感谢您的时间。如果帐户的位置相同,您可能需要更改LDAP路径以指向正确的位置。默认情况下,Windows中的用户在名为“用户”的CN中创建。我们通常将用户存储在一个名为“地理位置”的OU中。如果path语句没有指向正确的位置,将找不到用户帐户。问题是它非常旧,而且不是最新的。到目前为止,模拟管理员用户意味着凭据需要传递,这并不理想,因为它们需要保护。
        using System.DirectoryServices;

        // Impersonate the Admin to Reset the Password / Unlock Account //
        // Change variables below.
        ImpersonateUser iu = new ImpersonateUser();
        if (iu.impersonateValidUser("AdminUserName", "DomainName", "AdminPassword"))
        {
            resetPassword("AdminUserName", "AdminPassword", UserToReset, "NewPassword");
            iu.undoImpersonation();
        }

        // Perform the Reset / Unlock //
        public void resetPassword(string username, string password, string acct, string newpassword)
        {
            string Path = // LDAP Connection String
            string Username = username;
            string Password = password;
            string Domain = "DomainName\\"; // Change to your domain name

            DirectoryEntry de = new DirectoryEntry(Path, Domain + Username, Password, AuthenticationTypes.Secure);

            DirectorySearcher ds = new DirectorySearcher(de);

            ds.Filter = "(&(objectClass=user)(|(sAMAccountName=" + acct + ")))";

            ds.PropertiesToLoad.Add("displayName");
            ds.PropertiesToLoad.Add("sAMAccountName");
            ds.PropertiesToLoad.Add("DistinguishedName");
            ds.PropertiesToLoad.Add("CN");

            SearchResult result = ds.FindOne();

            string dn = result.Properties["DistinguishedName"][0].ToString();

            DirectoryEntry uEntry = new DirectoryEntry("LDAP://" + dn, username, password);

            uEntry.Invoke("SetPassword", new object[] { newpassword });
            uEntry.Properties["LockOutTime"].Value = 0;
            uEntry.CommitChanges();
            uEntry.Close();
        }
try
{
   path = path_to_your_executable;

   ProcessStartInfo myProcess = new ProcessStartInfo(path);
   myProcess.Domain = domain;
   myProcess.UserName = username;
   myProcess.Password = password;
   myProcess.UseShellExecute = false;

   Process.Start(myProcess);
}
catch (Exception myException)
{
   // error handling
}
if (iu.impersonateValidUser("AdminUserName", "DomainName", "AdminPassword"))
{            
    // Do Something Under Other Users Security Context
    iu.undoImpersonation();
}
using System;
using System.Runtime.InteropServices;
using System.Security.Principal;


public class ImpersonateUser
{
    public const int LOGON32_LOGON_INTERACTIVE = 2;
    public const int LOGON32_PROVIDER_DEFAULT = 0;

    WindowsImpersonationContext impersonationContext;

    [DllImport("advapi32.dll")]
    public static extern int LogonUserA(String lpszUserName,
        String lpszDomain,
        String lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        ref IntPtr phToken);
    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int DuplicateToken(IntPtr hToken,
        int impersonationLevel,
        ref IntPtr hNewToken);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool RevertToSelf();

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern bool CloseHandle(IntPtr handle);

    public bool impersonateValidUser(String userName, String domain, String password)
    {
        WindowsIdentity tempWindowsIdentity;
        IntPtr token = IntPtr.Zero;
        IntPtr tokenDuplicate = IntPtr.Zero;

        if (RevertToSelf())
        {
            if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
                LOGON32_PROVIDER_DEFAULT, ref token) != 0)
            {
                if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                {
                    tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                    impersonationContext = tempWindowsIdentity.Impersonate();
                    if (impersonationContext != null)
                    {
                        CloseHandle(token);
                        CloseHandle(tokenDuplicate);
                        return true;
                    }
                }
            }
        }
        if (token != IntPtr.Zero)
            CloseHandle(token);
        if (tokenDuplicate != IntPtr.Zero)
            CloseHandle(tokenDuplicate);
        return false;
    }

    public void undoImpersonation()
    {
        impersonationContext.Undo();
    }
}