1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
using System.Security;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
public static bool Authenticate(string domain, string username, string password)
{
SecureString pwd = new SecureString();
bool bAuth = false;
DirectoryEntry entry = null;
//Durchlaufe das Passwort und hänge es dem SecureString an
foreach (char c in password)
{
pwd.AppendChar(c);
}
//Bewirkt, dass das Passwort nicht mehr verändert werden kann
pwd.MakeReadOnly();
//Passwort wird einem Pointer übergeben, damit dieser später "entschlüsselt" werden kann
IntPtr pPwd = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(pwd);
try
{
entry = new DirectoryEntry(string.Concat(@"LDAP://", domain), username, System.Runtime.InteropServices.Marshal.PtrToStringBSTR(pPwd));
object nativeObject = entry.NativeObject;
bAuth = true;
}
catch (Exception)
{
bAuth = false;
}
finally
{
entry.Close();
entry.Dispose();
}
return bAuth;
}
|