C# UserPrincipal仅返回登录的用户
我正在从事一个项目,希望它能够允许用户在Active Directory和我们使用的其他一些应用程序中进行单击创建 我正在和C# UserPrincipal仅返回登录的用户,c#,.net,active-directory,C#,.net,Active Directory,我正在从事一个项目,希望它能够允许用户在Active Directory和我们使用的其他一些应用程序中进行单击创建 我正在和 using System.DirectoryServices.AccountManagement; 名称空间和 UserPrinciple 反对 我遇到的问题是,即使我将另一个用户传递到该方法中,它也只会返回已登录的用户 这里有一些代码 用于将用户添加到新组 public bool AddUserToGroup(string sUserName, string sGr
using System.DirectoryServices.AccountManagement;
名称空间和
UserPrinciple
反对
我遇到的问题是,即使我将另一个用户传递到该方法中,它也只会返回已登录的用户
这里有一些代码
用于将用户添加到新组
public bool AddUserToGroup(string sUserName, string sGroupName)
{
try
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
Trace.WriteLine(oUserPrincipal.Context);
GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);
if (oGroupPrincipal != null)
{
if (!IsUserGroupMember(sUserName, sGroupName))
{
oGroupPrincipal.Members.Add(pp);
oGroupPrincipal.Save();
}
}
return true;
}
catch
{
return false;
}
}
这是GetUser
UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
return oUserPrincipal;
但它只返回已登录的用户
如果您需要更多信息,请告诉我
我期待得到一些答复:
马汀
编辑PrincipalContext方法
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);
return oPrincipalContext;
我发现您必须将帐户类型传递给GetUser方法 原来是这样
UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
return oUserPrincipal;
现在是这样
UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, IdentityType.SamAccountName, sUserName);
return oUserPrincipal;
如果您不指定
IdentityType.SamAccountName
它将在当地寻找
Martyn你能分享你实例化oPrincipalContext的代码吗?更新了我的主要问题