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

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...

Enum types, FlagAttribute & Zero value

Enum types, FlagAttribute & Zero value : Enum types, FlagAttribute & Zero value: We all know about Enums types and use them every single day. What is not that often used is to decorate the Enum type with the FlagsAttribute. When an Enum type has the FlagsAttribute we can assign multiple values to it and thus combine multiple information into a single enum. The enum values should be a power of two so that a bit set is achieved. Here is a typical Enum type: public enum OperationMode { /// <summary> /// No operation mode /// </summary> None = 0, /// <summary> /// Standard operation mode /// </summary> Standard = 1, /// <summary> /// Accept bubble requests mode /// </summary> Parent = 2 } In such scenario no values combination are possible. In the following scenario a default operation mode exists and combination is used: [Flags] public enum OperationMode { /// <summary> /// Asynchronous operation mode //...