C# 在没有域的显式主体上下文的情况下构造扩展用户主体

C# 在没有域的显式主体上下文的情况下构造扩展用户主体,c#,directoryservices,account-management,principalcontext,C#,Directoryservices,Account Management,Principalcontext,最近,我一直在实现类似于示例的定制extendederUserPrincipal类。我唯一的问题是基于基类的现有实例(例如,UserPrincipal.Current)构造新实例,以将现有用户主体映射到我的扩展版本 现在,我使用显式创建域上下文: var ctx = new PrincipalContext(ContextType.Domain, "domain.name"); return ExtendedUserPrincipal.FindByIdentity(ctx, Ident

最近,我一直在实现类似于示例的定制
extendederUserPrincipal
类。我唯一的问题是基于基类的现有实例(例如,
UserPrincipal.Current
)构造新实例,以将现有用户主体映射到我的扩展版本
现在,我使用显式创建域上下文:

var ctx = new PrincipalContext(ContextType.Domain, "domain.name");
return ExtendedUserPrincipal.FindByIdentity(ctx,
     IdentityType.Sid, user.Sid.ToString());
我想做什么来构造它而不显式地指定域上下文,例如

return Extended.UserPrincipal.FindByIdentity(SomeHelper.GetDomainContext(user), 
     user.Sid, user.Sid.ToString());

user.Context
在这里没有帮助。这是设计可能的还是禁止的(可能是安全问题)?

假设.net framework版本为3.5或更高版本:难道
user.Context
不够吗?是的,
user.Context
是一个有效的解决方案!非常感谢。最初我拒绝了它,因为其中一个测试失败(构造的主体不是某个组的成员-但这是测试中的错误,
IsMemberOf
不支持嵌套组),但现在它可以了!。你能把这个贴出来让我接受吗?