ASP.NET MVC Forms Authentication with Active Directory

This is a very basic example of how to integrate Active Directory Authentication using ASP.NET MVC. This is very easy and requires only about 10 minutes to accomplish.

ASP.NET MVC Templates already have the required code and configuration to use the Authentication, so all we need to do is change the configuration to point to our Active Directory instead of SQL Express.

To do this you will need an accessible Active Directory and Visual Studio with MVC templates installed. I won’t show you how to install and configure AD here as I do not have this knowledge, yet!

  1. Create a new ASP.NET MVC Web Application
  2. Visual Studio will ask you if you want to create a test project, I usually work in TDD so I always say yes. You should now have 2 projects in your solution.
  3. If you open the Web.Config file and locate the <connectionStrings>, you will notice there is already a connection; you can remove it and add a new one for you Active Directory.
    4
  4. Now locate the tags authentication, membership, profile, roleManager and remove them (or comment out). Insert these
    5
  5. Now run the solution and try it using an existing user. You will need to use username@domain for the authentication to work.

This entry was posted on Monday, May 4th, 2009 at 9:15 am and is filed under ASP.NET, Active Directory, C#.NET, Developement, MVC. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

17 Responses to “ASP.NET MVC Forms Authentication with Active Directory”

» AZAM, ADAM, Abracanabram » ! Is Harry Potter a dotnet developer? Part 1 MembershipProvider blog.dervalp.com May 12th, 2009 at 3:53 pm

[...] If you only need an active directory authentication with no role managment, here is a post of my friend Mike who explains how to use Active Directory for the authentication with the new framework MVC (http://helios.ca/2009/05/04/aspnet-mvc-forms-authentication-with-active-directory/). [...]

DotNetShoutout May 28th, 2009 at 11:06 pm

ASP.NET MVC Forms Authentication with Active Directory | MIKE…

Thank you for submitting this cool story – Trackback from DotNetShoutout…

ASP.NET MVC Archived Buzz, Page 1 May 28th, 2009 at 11:55 pm

[...] to Vote[FriendFeed] ASP.NET MVC Forms Authentication with Active Directory | MIKE (5/28/2009)Thursday, May 28, 2009 from [...]

May 30th Links: ASP.NET, AJAX, ASP.NET MVC, Visual Studio - ScottGu's Blog May 30th, 2009 at 6:40 pm

[...] ASP.NET MVC Forms Authentication with Active Directory: Mike has a nice post that shows how to setup an ASP.NET MVC application with Forms Authentication that uses Active Directory as the username/password credential store instead of a database.  Also check out his post on ASP.NET MVC Forms Authentication with SQL Membership to learn more about how to setup forms authentication using a SQL Server database (instead of the default SQL Express one). [...]

BusinessRx Reading List : May 30th Links: ASP.NET, AJAX, ASP.NET MVC, Visual Studio May 30th, 2009 at 6:47 pm

[...] ASP.NET MVC Forms Authentication with Active Directory: Mike has a nice post that shows how to setup an ASP.NET MVC application with Forms Authentication that uses Active Directory as the username/password credential store instead of a database.  Also check out his post on ASP.NET MVC Forms Authentication with SQL Membership to learn more about how to setup forms authentication using a SQL Server database (instead of the default SQL Express one). [...]

Sergio June 2nd, 2009 at 4:04 pm

This is nice, but it is a simple scenario. Is it possible to use Forms Auth and a role provider for AD so that we can authorize the user from a specified role? E.g. [Authorize(Roles="Admin, SuperAdmin")].

Thanks

Mike June 2nd, 2009 at 5:15 pm

Hello Sergio, I’m currently building an application that requires ActiveDirectory authentication and role management from AD groups. Correct me if i’m wrong, but there is no out of the box solution for this.

I haven’t had time to look into it very much so far, but I plan on doing something similar to the [Authorize(Roles="admin"]attribute so that I could change my data source from AD to SQL Server easily.

I’ll blog about it once I’ve done something!

Sergio June 3rd, 2009 at 5:01 am

As far as I know it can be specified an AD role provider like this:

This works fine on Windows Authentification, but I’m not sure if it works on forms auth.

Enlaces de Mayo: ASP.NET, AJAX, ASP.NET MVC, Visual Studio « Thinking in .NET June 7th, 2009 at 9:41 am

[...] Autenticación por formulario con Active Directory en ASP.NET MVC: Mike nos muestra cómo configurar una apalicación ASP.NET MVC con autenticación por forumlario que usa Active Directory como credenciales en lugar de una base de datos. Leed también su post sobre el mismo tema, pero usando SQL Membership en lugar de Active Directory (en lugar del por defecto que es con SQL Express). [...]

May 30th Links: ASP.NET, AJAX, ASP.NET MVC, Visual Studio | rapid-DEV.net June 14th, 2009 at 12:38 pm

[...] ASP.NET MVC Forms Authentication with Active Directory: Mike has a nice post that shows how to setup an ASP.NET MVC application with Forms Authentication that uses Active Directory as the username/password credential store instead of a database.  Also check out his post on ASP.NET MVC Forms Authentication with SQL Membership to learn more about how to setup forms authentication using a SQL Server database (instead of the default SQL Express one). [...]

May 30th Links: ASP.NET, AJAX, ASP.NET MVC, Visual Studio | rapid-DEV.net June 14th, 2009 at 1:19 pm

[...] ASP.NET MVC Forms Authentication with Active Directory: Mike has a nice post that shows how to setup an ASP.NET MVC application with Forms Authentication that uses Active Directory as the username/password credential store instead of a database.  Also check out his post on ASP.NET MVC Forms Authentication with SQL Membership to learn more about how to setup forms authentication using a SQL Server database (instead of the default SQL Express one). [...]

Pavani July 24th, 2009 at 4:41 am

i’m using the same code. but when i’m tring to login it throws an error message like this :
Your login attempt was not successful. Please try again.

What i supposed to do?

Mike July 24th, 2009 at 8:36 am

Make sure you are using username@domain to login.

If it still doesn’t work you can try using domain\username.

dan July 31st, 2009 at 9:42 am

still can’t get this to work, would someone be kind enough to email me the source code for a few of the files?

tx

Mike August 2nd, 2009 at 12:33 pm

@DAN: There is no actual source code. This is the MVC Template source code with the proper configuration. You might want to ask some help from your Network Administrator, or the person who can give you the proper LDAP connection string and the right credentials. Once you have these information you should be able to make this work.

Pooja November 17th, 2009 at 2:12 am

Hi,
I want to use form atthentication in my website but i have one problem that folders are not managed properly according to role.already i have implemented custom security to manage roles but i m unable to stop access of annonomous users to access the page .can anyone tell me what should i do?

links for 2009-12-24 « 2LeggedSpider December 24th, 2009 at 8:02 am

[...] ASP.NET MVC Forms Authentication with Active Directory | MIKE (tags: aspnetmvc, authentication) [...]

Leave a Reply