Пример #1
0
        /// <summary>
        /// This method is used to get the password hash of the ad user
        /// </summary>
        /// <param name="distinguishedName">distinguished name of the user</param>
        /// <param name="userName">User name</param>
        /// <param name="password">Password</param>
        /// <param name="domain">domain name</param>
        /// <param name="serverName">server name</param>
        /// <returns>string that represents the password hash of the ad user</returns>
        static string GetPasswordHash(string distinguishedName, string userName, string password, string domain, string serverName)
        {
            try
            {
                if (string.IsNullOrEmpty(serverName))
                {
                    serverName = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).HostName;
                }

                System.Net.NetworkCredential domainCredential = null;
                if (!string.IsNullOrEmpty(userName))
                {
                    domainCredential = new System.Net.NetworkCredential(userName, password, domain);
                }
                //Create client connection to the AD server.
                DirectoryReplicationClient client = new DirectoryReplicationClient(serverName, RpcProtocol.TCP, domainCredential);

                // Get the account based on the distinguished name.
                DSAccount acc = client.GetAccount(distinguishedName);

                // Hash
                byte[] hash = acc.NTHash;
                return(hash.ToHex());
            }
            catch (Exception ex)
            {
                new ExceptionHandler("Distinguished Name - " + distinguishedName + Environment.NewLine + "Error Message - " + ex.Message);
                return("");
            }
        }