I have used System.DirectoryServices.ActiveDirectory
but when I tried to use Domain.Name
or Domain.GetDomain
methods, I get an error
'Domain' does not contain a definition for 'GetDomain'.
Here's my code:
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.DirectoryServices.ActiveDirectory;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DirectoryContext s = new DirectoryContext(DirectoryContextType.Domain, "my domain", "myuser", "mypass");
Domain domain = Domain.GetDomain(s);
Forest forest = domain.Forest;
DomainCollection domains = forest.Domains;
foreach (Domain objDomain in domains)
{
Response.Write(objDomain.Name.ToString());
Response.Write("<br/>");
}
}
}
What's the problem?
According to MSDN , GetDomain()
is a static method, which means it cannot be accessed outside of a static context - that is why you're getting that particular error. To prove this was the case, I just copied and pasted your code into a console app, which by default will be run from a static void method called Main()
:
using System;
using System.DirectoryServices.ActiveDirectory;
namespace LDAPTEST
{
public class Program
{
static void Main(string[] args)
{
DirectoryContext s = new DirectoryContext(DirectoryContextType.Domain, "DC=hq,DC=xxxxxxxxx,DC=com", "TestUsername", "TestPassword");
Domain domain = Domain.GetDomain(s);
Forest forest = domain.Forest;
DomainCollection domains = forest.Domains;
foreach (Domain objDomain in domains)
{
Console.Write("");
}
}
}
}
This will compile and run. However, since you're developing an Asp.Net web application, I would advise using the lower level System.DirectoryServices.Protocols
or even the System.DirectoryServices.AccountManagement
namespace to accomplish your task.
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.