Article ID: 329986 - Last Review: May 19, 2005 - Revision: 5.2 How to use the System.DirectoryServices namespace in ASP.NETThis article was previously published under Q329986 SUMMARYIntroductionThis article addresses problems that occur when an application that is written in Visual Studio .NET by using the System.DirectoryServices namespace works properly from a Windows-based or command line application but does not succeed in ASP.NET. If you experience this symptom, the problem pertains to security. The System.DirectoryServices namespace uses Active Directory Services Interfaces (ADSI) to contact discrete directory services by means of the different ADSI providers.This article assumes that you, the application designer, want the directory to be contacted under the security context of the ASP.NET Web user. If you do not want to do that, or you do not want to perform the resolutions that are listed in this article, you can work around these problems by passing credentials to your DirectoryServices code through the class constructor, or by using the Username and Password properties. MORE INFORMATIONWhat is a Primary Token?The Active Directory (AD) relies on the security mechanism of the Windows 2000 server. To access most information in the AD, you must provide credentials to the Windows 2000 server when requesting the AD information. The credentials you provide must be in a primary token, which just means that the IIS server has a password (not just a hash of the password) to pass to the AD.Double-Hop IssueThe double-hop issue is when the ASPX page tries to use resources that are located on a server that is different from the IIS server. In our case, the first "hop" is from the web browser client to the IIS ASPX page; the second hop is to the AD. The AD requires a primary token. Therefore, the IIS server must know the password for the client to pass a primary token to the AD. If the IIS server has a secondary token, the NTAUTHORITY\ANONYMOUS account credentials are used. This account is not a domain account and has very limited access to the AD.The double-hop using a secondary token occurs, for example, when the browser client is authenticated to the IIS ASPX page by using NTLM authentication. In this example, the IIS server has a hashed version of the password as a result of using NTLM. If IIS turns around and passes the credentials to the AD, IIS is passing a hashed password. The AD cannot verify the password and, instead, authenticates by using the NTAUTHORITY\ANONYMOUS LOGON. On the other hand, if your browser client is authenticated to the IIS ASPX page by using Basic authentication, the IIS server has the client password and can make a primary token to pass to the AD. The AD can verify the password and does authenticate as the domain user. For more information, click the following article number to view the article in the Microsoft Knowledge Base: 264921
(http://support.microsoft.com/kb/264921/
)
How IIS authenticates browser clients
How to Acquire a Primary TokenIf the IIS server has a primary token to pass on, the IIS server can pass a primary token to the AD on behalf of the client requesting the ASPX page. To acquire a primary token by using ASPX, use one of the following methods.Method AWhen the Web.config file is set to identity impersonate="true"/ and authentication mode="Windows", use the Anonymous account with the following settings:
Method BWhen Web.config and Machine.config are set as follows:
Error That You May Receive If You Do Not Have a Primary TokenIf the code works when you browse to it from the development machine that is a Web server, but the code does not work when other Web clients access the pages, you may receive an error message that is similar to one of the following:"Failed: System.Runtime.InteropServices.COMException (0x80005000):
Unknown error (0x80005000) at
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)" "The
specified directory service attribute or value does not exist"
Troubleshoot the Double-Hop IssueUse one of the following methods to troubleshoot the double-hop issue.Quick TestTo quickly determine whether this is a permissions issue, follow these steps:
Test Your CodeAnother good troubleshooting test for any IIS ASPX issue when you access the AD involves taking your ASPX code out of the IIS environment and running it as a script file on the IIS server itself. Follow these steps:
Event Type: Success Audit
Event Source: Security
Event Category: Directory Service Access
Event ID: 565
Date: 3/27/2002
Time: 3:21:41 PM
User: NT AUTHORITY\ANONYMOUS LOGON
Computer: TESTDC
Description:
Object Open:
Object Server: DS
Object Type: user
Object Name: CN=Users,DC=corp,DC=com
New Handle ID: 0
Operation ID: {0,68019232}
Process ID: 264
Primary User Name: TESTDC$
Primary Domain: TESTDOM
Primary Logon ID: (0x0,0x3E7)
Client User Name: ANONYMOUS LOGON
Client Domain: NT AUTHORITY
Client Logon ID: (0x0,0x40DE417)
Accesses READ_CONTROL
Privileges -
Properties:
264921
(http://support.microsoft.com/kb/264921/
)
How IIS authenticates browser clients
283201
(http://support.microsoft.com/kb/283201/
)
How to use delegation in Windows 2000 with COM+
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
317012
(http://support.microsoft.com/kb/317012/
)
Process and request identity in ASP.NET
ASP.NET Base AccountBy default, all ASP.NET applications run under the base process account, MACHINENAME\ASPNET. This is a local account that does not have access to objects in Active Directory. To access Active Directory by using the credentials that are passed to IIS, you must modify your Web.config file to contain the parameters identity impersonate="true" and authentication mode="Windows". The presence of these two parameters causes ASP.NET to run the code under the credentials that are passed to it by IIS.Note This is very similar to how classic ASP currently works. A High-Isolated or Out-of-Process (OOP) application is actually running in a separate DllHost process. DllHost's base process is IWAM_machinename. When calls are made to this OOP, it impersonates the user who was authenticated by IIS. With ASP.NET, pages will run in a separate process also, that process being Aspnet_wp.exe. By using the identity impersonate tag, the application designer controls whether that impersonation is performed. For more information, click the following article number to view the article in the Microsoft Knowledge Base: 317012
(http://support.microsoft.com/kb/317012/
)
Process and request identity in ASP.NET
Error That You May Receive If the ASP.NET Base Account Is Not Set CorrectlyIf the ASP.NET Base account is not set correctly, you may receive one of the following error messages:Cannot contact the
specified domain or domain does not exist Logon
Failure: Unknown Username or bad password Troubleshoot ASP.NET Base Account Not Set Correctly
ADSI Schema CacheADSI tries to cache the schema from the AD. The schema cache is used to determine how to read the attributes out of the attribute cache. If ADSI cannot cache the schema, it uses a V2 version of the schema. The V2 version of the schema contains a very small set of attributes. For more information, click the following article number to view the article in the Microsoft Knowledge Base:251189
(http://support.microsoft.com/kb/251189/
)
After locating an LDAP server
schema cached by ADSI
ADSI will try to cache the schema only one time per
process. In Windows 2000, ASP.NET runs under a single aspnet_wp.exe process.
This means that the schema will not be cached again until the IIS service is
shut down and restarted. Subsequent Schema cache access may depend on the user rights of the first user who runs an ASP.NET page that uses ADSI on that server. In a typical scenario, an administrator notices that the application works by launching a Web browser locally. The Web site is then made live and works for a period of time until the server is rebooted or Web services are restarted. At this point, the ASP.NET application stops working, because ADSI did not cache the schema correctly. This may happen when the first user to access the Web site cannot establish the credentials to correctly cache the schema. This is a likely scenario when a user suffers from the double-hop problem described earlier in this article. You may not quickly realize that this has happened because you may not see either of the following error messages: Permission Denied Property not Found in Cache The schema that ADSI uses is stored in the cn=Aggregate object in the schema namespace. Neither the Pre-Windows 2000 Compatible Access built-in group nor the Everyone principal have permissions on this aggregate object. Therefore, schema information is not accessible. The result is that there is a property in the attribute cache that was retrieved from the server that ADSI does not understand. Because ADSI cannot determine the data type, you receive the error that is mentioned in the next paragraph. Error That You May Receive When the ADSI Schema Cache Is Not AvailableYou may receive errors. After you restart the server, the Web application does not respond, and you may also receive the following error message:0x8000500C, "The property in cache cannot be
converted from native datatype" Troubleshoot the ADSI Schema CacheIf you receive the error that is mentioned in the previous paragraph, ADSI is not correctly caching your Active Directory schema. For more information and to verify whether the registry key was made and whether the file was written as described in another article, click the following article number to view the article in the Microsoft Knowledge Base:251189
(http://support.microsoft.com/kb/251189/
)
Locating an LDAP server schema cached by ADSI
Registry Keys and Resolving the Issue
REFERENCES264921
(http://support.microsoft.com/kb/264921/
)
How IIS authenticates browser clients
232714
(http://support.microsoft.com/kb/232714/
)
How to enable auditing of Directory Service access
283201
(http://support.microsoft.com/kb/283201/
)
How to use delegation in Windows 2000 with COM+
317012
(http://support.microsoft.com/kb/317012/
)
Process and request identity in ASP.NET
| Article Translations
|
Back to the top
