2012-02-03 27 views
3

我想在ActiveDirectory中添加用户。错误:当在AD中添加用户时出现“错误:用户不存在或不唯一”

我使用此代码

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); 

回答

相关问题