Click here to Skip to main content
12,894,708 members (57,923 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads

Stats

50.7K views
3.2K downloads
43 bookmarked
Posted 9 Oct 2011

Bypass Forms Authentication to Use Active Directory User Authentication in ASP.NET

, 9 Oct 2011 CPOL
This article describes how to keep form based and active directory user based authentication process in parallel in ASP.NET.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ActiveDirectoryAuthentication;
using System.Configuration;
using ActiveDirectoryAuthentication.Helper;

namespace ActiveDirectoryAuthentication
{
    public partial class Login : System.Web.UI.Page
    {
        private ActiveDirectoryConfiguration _currentActiveDirectoryConfiguration = null;

        protected bool IsActiveDirectoryEnabled
        {
            get
            {
                return ActiveDirectorySettings.Enabled;
            }
        }

        private static ActiveDirectoryConfiguration activeDirectorySettings = null;
        public static ActiveDirectoryConfiguration ActiveDirectorySettings
        {
            get
            {
                try
                {
                    if (activeDirectorySettings == null)
                    {
                        activeDirectorySettings = (ActiveDirectoryConfiguration)ConfigurationManager.GetSection("ldapConfiguration");
                    }
                }
                catch
                {
                }
                return activeDirectorySettings;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsActiveDirectoryEnabled)
            {
                LoginUser.Authenticate += new AuthenticateEventHandler(LoginUser_Authenticate);
            }
        }

        protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
        {
            try
            {
                if (IsActiveDirectoryEnabled)
                {
                    if (ActiveDirectoryConnector.IsUserLoggedIn(LoginUser.UserName, LoginUser.Password))
                    {
                        e.Authenticated = true;
                    }
                    else
                    {
                        e.Authenticated = false;
                    }
                }
            }
            catch (Exception ex)
            {
                e.Authenticated = false;
                LoginUser.FailureText = ex.Message;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Fazlur Rahman
Technical Lead Ominto Inc
United Arab Emirates United Arab Emirates
I am Bachelor in CSE from Khulna University of Engineering & Technology,Bangladesh. I have more than 11 years experience in software design & development, data analysis & modeling, project management and currently working in a software company in Dubai,UAE as a Lead Software Engineer. I am MCAD(Microsoft Certified Application Developer) certified since 2005. Please feel free to contact with me at nill_akash_7@yahoo.com.


You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 9 Oct 2011
Article Copyright 2011 by Fazlur Rahman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid