Tuesday

Code - LDAP Authentication Using AD and LDS in .NET 3.5 and Higher

With the introduction of .NET 3.5, came a new library in .Net framework called System.DirectoryServices.AccountManagement.dll. If you include this dll into your project then you can authenticate against AD or LDS in matter of two lines of code. Please pay attention to parameters provided to the constructor of PrincipalContext's instance in each case. Infact, these parameters define the difference between connecting each identity store (Active Directory and Lightweight Directory Service).

LDS is one of two identity providers that are supported by Active Directory Federation Services (AD FS) for authentication purposes and to supply claims to federation-aware Web applications, the other being AD.

AD Authentication:

using(PrincipalContext pCtx = new PrincipalContext(ContextType.Domain, "abc.com")) 
{
    bool isAuthenticated = pCtx.ValidateCredentials("username", "PaSsWoRd");
}



LDS Authentication:

using (PrincipalContext pCtx = new PrincipalContext(ContextType.ApplicationDirectory, "ldsserver:389", "DC=abc,DC=local")) 
{
      bool  isAuthenticated = pCtx.ValidateCredentials("username", "PaSsWoRd");
}

Objects mentiond in above code snippets are found in following namespace:

System.DirectoryServices.AccountManagement

The constructor of PrincipalContext also accepts another parameter of enumeration type called ContextOptions, which specifies the options to bind to the server. If you don't provide this parameter to the constructor then the default options are:

ContextOptions.Negotiate | ContextOptions.Signing | ContextOptions.Sealing

If you are a software developer or a software architect and work with directory-enabled applications then you must read the following article at MSDN:  http://msdn.microsoft.com/en-us/library/bb897400.aspx



No comments:

Post a Comment

Your comments are highly appreciated!