Skip to main content

Authenticate User on Active Directory

What is Active Directory

A directory service from Microsoft that is a part of Windows 2000. It is an implementation of Internet standard directory and naming protocols that uses a database engine for transactional support, and also supports a variety of application programming interface standards.

System.DirectoryServices Namespace

The System.DirectoryServices namespace provides easy access to Active Directory Domain Services from managed code. The namespace contains two component classes, DirectoryEntry and DirectorySearcher, which use the Active Directory Services Interfaces (ADSI) technology. ADSI is the set of interfaces that Microsoft provides as a flexible tool for working with a variety of network providers. ADSI gives the administrator the ability to locate and manage resources on a network with relative ease, regardless of the size of the network.

System.DirectoryServices.ActiveDirectory Namespace

The System.DirectoryServices.ActiveDirectory namespace provides a high level abstraction object model that builds around Microsoft Active Directory services tasks. The Active Directory service concepts such as forest, domain, site, subnet, partition, and schema are part of the object model. The System.DirectoryServices.ActiveDirectory namespace is used to automate Active Directory management tasks. System.DirectoryServices.ActiveDirectory is not used to access data that resides within Active Directory or any other directory service. The System.DirectoryServices namespace should be used for this purpose. The System.DirectoryServices.ActiveDirectory namespace is intended for use by application developers who are familiar with .NET Framework programming using Visual Basic .NET or C#. Knowledge of directory services programming is also helpful. System.DirectoryServices.ActiveDirectory is part of Microsoft Visual Studio 2005 and is supported on any operating system that Microsoft Visual Studio 2005 is compatible with. Many of the classes, methods, and properties in the System.DirectoryServices.ActiveDirectory namespace use the LinkDemand code access security option. This means that the code access security demand only occurs during just-in- time compilation and that the demand is performed only on the calling assembly and not up the entire call stack. Because of this, callers should not pass objects created from this namespace at runtime to untrusted code.

Use ActiveDirectory to check credential

     private void checkActiveDirectoryLogin(string strUsername, string strPassword)
        {
            bool isAuthenticated = false;
            System.DirectoryServices.DirectoryEntry Entry = new System.DirectoryServices.DirectoryEntry();
            System.DirectoryServices.DirectorySearcher Searcher = new System.DirectoryServices.DirectorySearcher();
            System.DirectoryServices.SearchResult Results = default(System.DirectoryServices.SearchResult); 
            string domainName = null;
            try
            {
                Entry.Path = "LDAP://" + domainName;// SERVER/USERGROUP
                Entry.Username = strUsername;
                Entry.Password = strPassword;
 
                Searcher.SearchRoot = Entry;
                Searcher.SearchScope = System.DirectoryServices.SearchScope.OneLevel;
 
                Results = Searcher.FindOne();
 
                isAuthenticated = (Results != null);
 
                if (isAuthenticated)
                {
                   //Code for Successful Login
                }
                else
                {
                    // Code for unsuccessful login
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Comments

Popular posts from this blog

"Time-Saving VS11 and ASP.NET 4.5 Features You Shouldn’t Miss” ebook by Telerik

“Time-Saving VS11 and ASP.NET 4.5 Features You Shouldn’t Miss” ebook by Telerik : Here is a nice opportunity offered by Telerik : They have created this amazing new ebook that you can download for free. More than that, you don’t even need to register to download the pdf, so it is free free! What’s inside ? Intellisense help you discover JS and CSS features WAI Aria makes the web usable by all HTML5 and CSS3 are available out of the box WebAPI exposes data via HTTP service Strongly Typed Data Binding ensures runtime confidence Request Validation prevents yellow screens of death Page Inspector puts an end to layout headaches Download it here: Free E-book: Time-Saving VS11 and ASP.NET 4.5 Features You Shouldn’t Miss I strongly recommend this reading, you will not loose your time and will learn something new for sure!

jQuery Tip #1 – Defining a Context When Using Selectors

jQuery Tip #1 – Defining a Context When Using Selectors : jQuery Tip #1 – Defining a Context When Using Selectors: I really enjoy working with jQuery and have had the opportunity to use it a lot over the years on various projects.  It’s an essential part of my “technology tool belt”. My company has also started providing new training classes on jQuery to various companies and I’ve had a lot of great questions come up about best practices, tips and tricks, when certain functions should be used over other functions, and more. I decided to put together a series of posts that highlight simple tips and tricks that I’ve used in jQuery applications over the years to provide some guidance to developers new to jQuery and provide answers to different questions I’ve been asked. In this first post I’ll cover an oldie but goodie tip – defining a context when using selectors. It’s something I struggled with when I first started using jQuery but once I found the secret (which is quite easy) i...