C# 必须先保留主体对象,然后才能调用此方法#
我试图从广告中获得额外的用户信息C# 必须先保留主体对象,然后才能调用此方法#,c#,C#,我试图从广告中获得额外的用户信息 List<string> allUsers = new List<string>(); PrincipalContext ctx2 = new PrincipalContext(ContextType.Domain, "FUNDACION", valor); UserPrincipal qbeUser2 = new UserPrincipal(ctx2);
List<string> allUsers = new List<string>();
PrincipalContext ctx2 = new PrincipalContext(ContextType.Domain, "FUNDACION", valor);
UserPrincipal qbeUser2 = new UserPrincipal(ctx2);
qbeUser2.Enabled = true; // activo para autenticacion
PrincipalSearcher srch2 = new PrincipalSearcher(qbeUser2);
srch2.QueryFilter = qbeUser2;
DirectoryEntry dirEntry = (qbeUser2.GetUnderlyingObject() as DirectoryEntry);
List alluser=new List();
PrincipalContext ctx2=新PrincipalContext(ContextType.Domain,“FUNDACION”,valor);
UserPrincipal qbeUser2=新用户主体(ctx2);
qbeUser2.Enabled=true;//自动激活
PrincipalSearcher srch2=新PrincipalSearcher(qbeUser2);
srch2.QueryFilter=qbeUser2;
DirectoryEntry dirEntry=(作为DirectoryEntry的qbeUser2.GetUnderlineingObject());
我在这一行遇到了一个例外:
DirectoryEntry dirEntry=(作为DirectoryEntry的qbeUser2.GetUnderlineingObject())
System.DirectoryServices.AccountManagement.dll中发生“System.InvalidOperationException”类型的异常,但未在用户代码中处理
附加信息:在调用此方法之前,必须保留主体对象
您是否可以访问
UserPrincipal
类的源代码,特别是它的getunderyingobject()
方法
该方法似乎正在引发异常。这可能是合同造成的,也可能是偶然造成的
如果期望抛出此异常(“按约定”),则应将此方法调用包装在try/catch
块中,并相应地处理异常:
try {
DirectoryEntry dirEntry = (qbeUser2.GetUnderlyingObject() as DirectoryEntry);
} catch (InvalidOperationException e) {
// handle expected exception accordingly
}
否则,您应该确保在调用该方法之前保存该对象,正如异常消息所说():
您需要先执行qbeUser2.Save(),然后才能调用GetUnderlyingObject()如何知道对象是否已持久化?主体对象是在调用方法之前创建的。如果它是刚刚创建的,那么它可能还没有被持久化,因为它只存在于内存中。您应该调用一些例程来保持它,但是哪一个例程是,只有您知道(或者至少应该知道)。您正在使用的DLL是由谁编写的?“您有权访问UserPrincipal类的源代码,特别是其GetUnderlineObject()方法吗?”,不幸的是,该DLL不可用,我不知道这是一个.NET程序集。快速搜索显示该对象有一个
Save()
方法,我编辑了我的答案,添加了一个指向文档的链接。
qbeUser2.Save();
DirectoryEntry dirEntry = (qbeUser2.GetUnderlyingObject() as DirectoryEntry);