16 December 2011

Validate Login User with AD/LDAP authentication(Login Page)

Validating users against Active Directory/ LDAP. Also many organisation have multiple domains and same application needs to validate accross all domain.

This code can be used in SharePoint custom Login form for user Validation for Claim based authentication or Form Based Authentication.




Reference Added :

using System.Runtime.InteropServices;

COMException : The exception that is thrown when an unrecognized HRESULT is returned from a COM method call for more simplified error response from LDAP Error: Unknown error (0x80005000).


using System.DirectoryServices;


Below is the code sniplet

using (DirectoryEntry entry = new DirectoryEntry())
{
entry.Username = "DOMAIN\\LOGINNAME";
entry.Password = "PASSWORD";
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(objectclass=user)";
try
{
searcher.FindOne();
{
//Add Your Code if user Found..
}
}
catch (COMException ex)
{
if (ex.ErrorCode == -2147023570)
{
ex.Message.ToString();
// Login or password is incorrect
}
}
}


ErrorCode : -2147023570 suggest the Username or password is not correctly entered.

post your questions, comments or suggestion.


2 comments:

  1. hi,
    if i write this much code will it work for active directory users are anything extra i need to add. can u help me

    ReplyDelete
    Replies
    1. This code is sufficient to validate AD users against multiple LDAP
      Thanks Akshaya

      Delete

Thanks for your valuable comments

Rate Now: