views:

41

answers:

2

In my website i want to use active directory users for authentication. how can i do this.

+2  A: 

You need to specify Windows Authentication in your web.config

<system.web>
    <authentication mode="Windows"/>
</system.web>

Then set up allow/deny blocks to specify users who have access, etc.

<authorization>
  <allow roles="AuthorizedADGroup" />
  <allow users="AllowedUserName" />
  <deny users="*" />
  <deny users="?"/>      
</authorization>
Tim Coker
+1  A: 

If you need to do programmatic validation of credentials against Active Directory, you should use the new System.DirectoryServices.AccountManagement classes that are available in .NET 3.5.

Read the Managing Directory Security Principals in the .NET Framework 3.5 on MSDN Magazine for more info.

For validating credentials, you'd have to create a principal context - either a machine (single server) or domain (network) and then call the .ValidateCredentials() method on it:

using System.DirectoryServices.AccountManagement;

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");

if(ctx.ValidateCredentials(userName, password))
{
    // user is validated
}

Pretty simple, isn't it?? This works great if your users need to log in using a form where they enter username and password and you can grab these to check their account.

marc_s