简体   繁体   中英

Integrating Active Directory in my asp.net website

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

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>

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 in the January 2008 issue of MSDN Magazine for more info. NOTE: only CHM download

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM