probeer een ldap-server te benaderen vanaf een iis-server in een dmz en krijg het foutbericht: Informatie over het domein kon niet worden opgehaald (1355). Er zijn artikelen over het toevoegen van dns-informatie of het gebruik van de onderliggende objecten , maar deze oplossingen werken niet voor mij, dus onthoud me van Google zoeken en plaatsen van hetzelfde slechte advies.
Ik heb de hele laag opnieuw geschreven om hoofdobjecten te gebruiken. Dat bracht me op zijn minst een goede foutmelding.
using System;
using System.Collections;
using System.Collections.Generic;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Web;
using TheDomain.Common.Extensions;
namespace MobileApplications
{
public class Ldap35: IDisposable
{
private string _ldapserver;
private string _adminUser;
private string _adminPassword;
private PrincipalContext _connection;
private UserPrincipal _userData;
private IList _groups;
public delegate void MessagingHandler(string message);
public event MessagingHandler Messaged;
public Ldap35(string server, string adminuser, string adminpassword)
{
_ldapserver = server;
_adminPassword = adminpassword;
_adminUser = adminuser;
}
///
/// this will basically instantiate a UserPrincipal
///
/// just the user
/// just the password
/// the correct domain, not sure if this is thedoamin.com or the_domain
///
public bool Authenticate(string username, string pass, string domain)
{
if ( _connection == null)
EstablishDirectoryConnection();
ValidateConnection();
if (!domain.IsEmpty() && !username.Contains("\\") && !username.Contains("/"))
username = domain + "\\" + username;
_userData = UserPrincipal.FindByIdentity(_connection, username);
if (_userData == null)
throw new ApplicationException("Unable to locate user.");
if (! _connection.ValidateCredentials(username, pass))
throw new ApplicationException("Invalid credentials. Unable to log in.");
//_userData = new UserPrincipal( _connection, username, pass, true );
return true;
}
public bool Authenticate(string username, string pass)
{
return Authenticate(username, pass, "");
}
public bool IsMemeberOfGroup(string group)
{
ValidateConnection(); ValidateUser();
return _userData.IsMemberOf(new GroupPrincipal(_connection));
}
public bool IsMemeberOfGroup(string group, bool caseSensitive)
{
if (caseSensitive)
return IsMemeberOfGroup(group);
GetGroups();
return _groups.Any(g => g.ToLower().Trim() == group.ToLower().Trim());
}
// public IList GetGroups()
// {
// if ( _groups == null )
// _groups = new List();
//
// ValidateConnection(); ValidateUser();
//
// var results = _userData.GetGroups();
//
// foreach (var principal in results)
// {
// _groups.Add(principal.Name);
// }
//
// return _groups;
// }
public IList GetGroups()
{
if (_groups == null)
_groups = new List();
ValidateConnection(); ValidateUser();
Print("Getting groups");
DirectoryEntry de = (DirectoryEntry)_userData.GetUnderlyingObject();
object obGroups = de.Invoke("Groups");
foreach (object ob in (IEnumerable)obGroups)
{
//Create object for each group.
var obGpEntry = new DirectoryEntry(ob);
Print(obGpEntry.Name);
_groups.Add(obGpEntry.Name);
}
return _groups;
}
///
/// PrincipalContext class to establish a connection to the target directory and specify credentials for performing operations against the directory. This approach is similar to how you would go about establishing context with the DirectoryContext class in the ActiveDirectory namespace.
///
/// a user with permissions on the domain controller
/// the password to go with the above
///
private void EstablishDirectoryConnection()
{
_connection = new PrincipalContext(ContextType.Domain, _ldapserver, "DC=thedomain,DC=com", ContextOptions.SimpleBind, _adminUser, _adminPassword);
}
private void Print(string message)
{
if (Messaged != null)
Messaged(message);
}
private void ValidateConnection()
{
if ( _connection == null)
throw new ApplicationException("No connection to server, please check credentials and configuration.");
}
private void ValidateUser()
{
if (_userData == null)
throw new ApplicationException("User is not authenticated. Please verify username and password.");
}
public void Dispose()
{
_userData.Dispose();
_connection.Dispose();
}
}
}