错误:在AD​​中添加用户时出现“错误:用户不存在或不唯一”

时间:2012-02-03 09:51:10

标签: sharepoint-2010

我想在ActiveDirectory中添加用户。

我使用此代码

private SPUser CreateUser(string strLoginName, string strEMail, 
 string strName, string strNotes, string strSiteURL)
{
SPUser spReturn = null;
SPSite spSite = null;
SPWeb spWeb = null;

try
{
//Open the SharePoint site
spSite     = new SPSite(strSiteURL);
spWeb     = spSite.OpenWeb();

//Assign role and add user to site
SPRoleAssignment spRoleAssignment = 
    new SPRoleAssignment(strLoginName, strEMail, strName, strNotes);
//Using Contribute, might need high access
SPRoleDefinition spSPRoleDefinition = 
    spWeb.RoleDefinitions["Contribute"]; 

spRoleAssignment.RoleDefinitionBindings.Add(spSPRoleDefinition);
spWeb.RoleAssignments.Add(spRoleAssignment);

//Update site
spWeb.Update();
spReturn = spWeb.AllUsers[strLoginName];
}
catch(Exception)
{ 
}
finally
{
spWeb.Close();
spSite.Close();
 }

 return spReturn;
  }

spWeb.RoleAssignments.Add(spRoleAssignment); 错误:“错误:用户不存在或不是唯一的”

修改

以下对我有用:

 SPUser user = spWeb.EnsureUser(strLoginName);
 SPRoleAssignment spRoleAssignment = 
 new SPRoleAssignment(user);

1 个答案:

答案 0 :(得分:0)

您的代码只会授予Contribute对现有Active Directory用户的访问权限。

如果您确实要创建新的Active Directory用户,请参阅Create Active Directory user in .NET (C#)