title: How to securely authenticate AD user in Powershell (encrypt credentials)
body: <p>My application requires a user to be authenticated against Active Directory. We are thinking of executing a PowerShell script using System.DirectoryServices.DirectoryEntry to which we pass a username and password.</p>
  
  <p>I saw it mentioned in a different answer the fact that System.DirectoryServices.DirectoryEntry uses LDAP to read AD information. LDAP protocol by itself is not encrypted. You can use LDAPS but that requires setting up of CA. I would like to know if the network traffic generated by this command is secure by default - i.e. is it possible for the password to be sniffed over the network?</p>
  
  <p>EDIT
  I have found that you can pass additional options to the DirectoryEntry instance. This is the sample code:</p>
  
  <pre><code>$username = $args[0]
  $password = $args[1]
  
  Function Test-ADAuthentication {
      param($username,$password)
      (new-object directoryservices.directoryentry "",$username,$password,Secure -bor Sealing).psbase.name -ne $null
  }
  
  Test-ADAuthentication $username $password
  </code></pre>
  
  <p>The fourth parameter is an enum AuthenticationTypes <a href="http://msdn.microsoft.com/en-us/library/system.directoryservices.authenticationtypes(v=vs.90).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.directoryservices.authenticationtypes(v=vs.90).aspx</a></p>
  
  <p>The values that seem of interest are: Secure &amp; Sealing which in combination will encrypt the credentials</p>
  
  <p>Many thanks for reading.</p>