我有一个已使用用户主体名称 (UPN) 格式保存在数据库中的帐户:[email protected]
我正在使用 UPN 格式对声明进行身份验证的 SharePoint 环境中工作。
我的问题是我需要获取持久 UPN 帐户的 UserProfile 对象。我已尝试以下方法,但不起作用:
string upnAccount = "[email protected]";
SPServiceContext ctx = SPServiceContext.GetContext(SPContext.Current.Site);
UserProfileManager upm = new UserProfileManager(ctx);
UserProfile user = upm.GetUserProfile(upnAccount);
我不断收到:Microsoft.Office.Server.UserProfiles.UserNotFoundException:检索用户配置文件时遇到错误
这是否意味着我必须将 UPN 帐户转换为索赔,如果是这样,有人有关于如何执行此操作的示例吗?
UserProfileManager UPM = null;
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
ServerContext serverContext = ServerContext.GetContext(site);
UPM = new UserProfileManager(serverContext);
foreach (UserProfile profile in UPM)
{
an = profile["AccountName"].Value;
title = profile["Title"].Value;
}
}
}
您可以尝试此方法来获取所有用户配置文件。在 foreach 循环中,您可以检查字段并获取特定用户的详细信息。
在某些情况下,在具有联合身份验证的 SharePoint 网站下,除非您未设置同步,否则不会自动创建用户配置文件。对于此问题,您可以通过 Central Admin 检查 [email protected] 的用户配置文件是否已存在。
此外,您的代码必须在模拟下运行,检查日志是否有异常并尝试使用 SPSecurity.RunWithElevatedPrivileges。