Using system;
Using system. Collections. Generic;
Using system. text;
Using system. directoryservices;
Namespace vqp. BLL
{
Public class ldapauthentication
{
Private string _ path;
Private string _ filterattribute = string. empty;
Public ldapauthentication ()
{
//
// Todo: Add constructor logic here
//
}
/// <Summary>
///
/// </Summary>
/// <Param name = "path"> </param>
Public ldapauthentication (string path)
{
_ Path = path;
}
/// <Summary>
///
/// </Summary>
/// <Param name = "Domain"> </param>
/// <Param name = "username"> </param>
/// <Param name = "PWD"> </param>
/// <Returns> </returns>
Public bool isauthenticated (string username, string
PWD)
{
Return isauthenticated (false, "", username, PWD );
}
/// <Summary>
///
/// </Summary>
/// <Param name = "Domain"> </param>
/// <Param name = "username"> </param>
/// <Param name = "PWD"> </param>
/// <Returns> </returns>
Public bool isauthenticated (bool includedomain, string domain, string username, string PWD)
{
Bool bresult = false;
String domainandusername = username;
If (includedomain)
Domainandusername = domain + @ "\" + username;
Directoryentry entry = new directoryentry (_ path,
Domainandusername,
PWD );
Try
{
// Bind to the native adsobject to force authentication.
Object OBJ = entry. nativeobject;
Bresult = true;
// Directorysearcher search = new directorysearcher (entry );
//
// Search. Filter = "(samaccountname =" + username + ")";
//
// Search. propertiestoload. Add ("cn ");
//
// Searchresult result = search. findone ();
//
/// Update the new path to the user in the directory
// If (result! = NULL)
//{
// _ Path = result. path;
//
// _ Filterattribute = (string) result. properties ["cn"] [0];
//
// Bresult = true;
//}
}
Catch (exception ex)
{
Throw new exception ("error Authenticating user." + ex. tostring ());
}
Return bresult;
}
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 merge sindex, commaindex;
For (INT propertycounter = 0; propertycounter <propertycount; propertycounter ++)
{
DN = (string) result. properties ["memberof"] [propertycounter];
Export sindex = DN. indexof ("=", 1 );
Commaindex = DN. indexof (",", 1 );
If (-1 = equalsindex)
{
Return NULL;
}
Groupnames. append (DN. substring (pipeline sindex + 1), (commaindex-pipeline sindex)-1 ));
Groupnames. append ("| ");
}
}
Catch (exception ex)
{
Throw new exception ("error obtaining group names." + ex. Message );
}
Return groupnames. tostring ();
}
}
}
/// <Summary>
///
/// </Summary>
/// <Param name = "struserid"> </param>
/// <Param name = "strpassword"> </param>
/// <Returns> </returns>
Public String validuserlogin (string struserid, string strpassword)
{
String strmessage = string. empty;
// Corp user has 8 characters, matpartners user must not be 8 characters.
String strpath = "LDAP: // matpartners ";
If (struserid. Length = 8)
Strpath = "LDAP: // Corp ";
String domain = "matpartners ";
If (struserid. Length = 8)
Domain = "corp ";
Try
{
Ldapauthentication objbll = new ldapauthentication (strpath );
If (! Objbll. isauthenticated (true, domain, struserid, strpassword ))
{
Strmessage + = "please check your name or password! ";
}
}
Catch (exception ex)
{
Strmessage + = "please check your name or password! ";
}
Return strmessage;
}