Authentication af Windows bruger op imod Active Directory.
Jeg vil gerne kunne tage den windows bruger der er logget ind på en client (vilkårlig maskine), og Authenticate ham op imod Active Directory på en server.Jeg kan fint "hardcode" værdierne (username, password), men jeg har brug for at hente dem for den bruger som er logget ind.
Lidt kode:
-----------------
public bool Authenticate(String domain, String username, String pwd)
{
String domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
try
{ //Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if(null == result)
{
return false;
}
//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
return false;
}
return true;
}
}
-------------
Jeg har fundet ud af hvordan man for fat i windows brugeren ved hjælp af:
----------------
//Get information on the current logged in user.
WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
LdapAuthentication ldapAuthentication = new LdapAuthentication("LDAP://minserver");
System.Console.WriteLine(ldapAuthentication.Authenticate("minserver", wp.Identity.Name;, "harcodetpassword-ahhh"));
-----------
Håber der er noget der kan hjælpe mig det sidste stykke.
