Windows User Login Validation

Source: Internet
Author: User
Tags findone ldap
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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.