Valider bruger via LDAP mod Active Directory
Har fundet et eksempel fra microsoft.com som jeg forsøger at få til at virke.Test opstillingen er som følger:
- Serveren som er Domain Controler (Windows 2003) med Active Directory. Serverens navn er "Server", domænet er "intranet".
- Min klient (den som jeg udvikler på) med VS.Net.
Jeg har en form, hvor jeg kan indtaste brugernavn og password. Når brugeren trykker på en knap udføres følgende event:
LdapAuthentication ldap = new LdapAuthentication("LDAP://DC=Server");
label1.Text = "Bruger er: " + ldap.IsAuthenticated("intranet", username.Text, password.Text).ToString();
Når jeg afvikler koden får jeg en exception ved: return true;
Der gives følgende exception:
An unhandled exception of type 'System.Exception' occurred in LDAP1.exe
Additional information: Error authenticating user. Enten findes det angivne domæne ikke, eller også kunne der ikke oprettes forbindelse til det
Jeg har ved brug af en netværkssniffer (Ethereal) set at der ikke er nogen kommunikation mellem min maskine og serveren.
Kan ikke selv se fejlen/årsagen, nogen som kan hjælpe?
LdapAuthentication klassen:
----------------------------------------------------
using System;
using System.Text;
using System.Collections;
using System.DirectoryServices;
namespace LDAP1
{
public class LdapAuthentication
{
private String _path;
private String _filterAttribute;
public LdapAuthentication(String path)
{
_path = path;
}
public bool IsAuthenticated(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;
//throw new Exception("Dette er en test");
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)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}
public String GetGroups()
{
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn=" + _filterAttribute + ")";
search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();
try
{
SearchResult result = search.FindOne();
int propertyCount = result.Properties["memberOf"].Count;
String dn;
int equalsIndex, commaIndex;
for(int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
{
dn = (String)result.Properties["memberOf"][propertyCounter];
equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if(-1 == equalsIndex)
{
return null;
}
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
groupNames.Append("|");
}
}
catch(Exception ex)
{
throw new Exception("Error obtaining group names. " + ex.Message);
}
return groupNames.ToString();
}
}
}
