在 SharePoint 2010 中允许 UserProfileManager 权限

发布于 2024-10-18 09:09:35 字数 1967 浏览 3 评论 0原文

我正在尝试使用 UserProfileManager 在自定义 Web 部件中显示用户列表。由于某种原因,我可以查看 Web 部件,并且所有配置文件都输出到屏幕上(也许因为我是管理员)。但当标准用户登录时,他们会遇到 403 页面。

我已经对此进行了一些阅读,我知道它与权限有关。这就是我的代码中的内容:

private DataTable GetProfiles()
    {
        DataTable dtUserProfile = new DataTable();
        //...DataTable Columns

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            Guid guid = SPContext.Current.Site.ID;

            using (SPSite intranet = new SPSite(guid))
            {
                SPUserToken userToken = intranet.Owner.UserToken;

                //Get current intranet context.
                SPServiceContext sContext = SPServiceContext.GetContext(intranet); 

                UserProfileManager profileManager = new UserProfileManager(sContext); 

                int totalUsers = int.Parse(profileManager.Count.ToString());

                Random random = new Random(); 

                for (int i = 0; i < NumberOfUsersToRetrieve(NoOfProfiles, totalUsers); i++)
                {
                    int randNumber = random.Next(1, totalUsers); 

                    DataRow drUserProfile; 

                    UserProfile up = profileManager.GetUserProfile(randNumber); 

                    drUserProfile = dtUserProfile.NewRow();

                    drUserProfile["DisplayName"] = up.DisplayName;
                    drUserProfile["FirstName"] = up["FirstName"].Value;
                    drUserProfile["LastName"] = up["LastName"].Value;
                    drUserProfile["Department"] = up["Department"].Value;
                    drUserProfile["ContactNumber"] = up["Office"].Value;                        
                    drUserProfile["MySiteUrl"] = up.PublicUrl;

                    dtUserProfile.Rows.Add(drUserProfile);
                }
            }
        }); 

        return dtUserProfile;
    }

我的代码基本上根据我想要返回的用户数量获取随机的用户集合。

是否可以为具有检索用户配置文件所需的所有权限的用户创建 SPUserToken?

谢谢!

I am trying to display a list of users in a custom webpart using the UserProfileManager. For some reason, I can view the webpart and all profiles are output to the screen (maybe because I am an administrator). But when a standard user logs in, they encounter a 403 page.

I have done some reading up on this and I know its something to do with permissions. This is what I have in my code:

private DataTable GetProfiles()
    {
        DataTable dtUserProfile = new DataTable();
        //...DataTable Columns

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            Guid guid = SPContext.Current.Site.ID;

            using (SPSite intranet = new SPSite(guid))
            {
                SPUserToken userToken = intranet.Owner.UserToken;

                //Get current intranet context.
                SPServiceContext sContext = SPServiceContext.GetContext(intranet); 

                UserProfileManager profileManager = new UserProfileManager(sContext); 

                int totalUsers = int.Parse(profileManager.Count.ToString());

                Random random = new Random(); 

                for (int i = 0; i < NumberOfUsersToRetrieve(NoOfProfiles, totalUsers); i++)
                {
                    int randNumber = random.Next(1, totalUsers); 

                    DataRow drUserProfile; 

                    UserProfile up = profileManager.GetUserProfile(randNumber); 

                    drUserProfile = dtUserProfile.NewRow();

                    drUserProfile["DisplayName"] = up.DisplayName;
                    drUserProfile["FirstName"] = up["FirstName"].Value;
                    drUserProfile["LastName"] = up["LastName"].Value;
                    drUserProfile["Department"] = up["Department"].Value;
                    drUserProfile["ContactNumber"] = up["Office"].Value;                        
                    drUserProfile["MySiteUrl"] = up.PublicUrl;

                    dtUserProfile.Rows.Add(drUserProfile);
                }
            }
        }); 

        return dtUserProfile;
    }

My code basically gets a random collection of users depending on the number of users I want to return.

Is it possible to create a SPUserToken for a user that all permissions needed to retrieve the user profiles?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新人笑 2024-10-25 09:09:35

我很高兴这个问题很老,但我也遇到了完全相同的问题。为了帮助原始发帖者和其他用户,我已将原始帖子中的代码更改为以下内容:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    SPSite sc = new SPSite(SPContext.Current.Site.ID);
    SPServiceContext context = SPServiceContext.GetContext(sc);

    HttpContext currentContext = HttpContext.Current;
    HttpContext.Current = null;

    UserProfileManager profileManager = new UserProfileManager(context);

    IEnumerator profileEnum = profileManager.GetEnumerator();

    while (profileEnum.MoveNext())
    {
        UserProfile up = (UserProfile)profileEnum.Current;

        if ((up["FirstName"] != null && up["FirstName"].Value != null && !String.IsNullOrEmpty(up["FirstName"].Value.ToString()))
            && (up.PublicUrl != null && !String.IsNullOrEmpty(up.PublicUrl.ToString())))
        {
            DataRow drUserProfile;

            drUserProfile = dtUserProfile.NewRow();

            drUserProfile["DisplayName"] = up.DisplayName;
            drUserProfile["FirstName"] = up["FirstName"].Value;
            drUserProfile["LastName"] = up["LastName"].Value;
            drUserProfile["Department"] = up["Department"].Value;
            drUserProfile["Location"] = up["SPS-Location"].Value;           

            drUserProfile["MySiteUrl"] = up.PublicUrl.ToString().Replace(@"\", @"\");

            dtUserProfile.Rows.Add(drUserProfile);
        }
    }
}

HttpContext.Current = currentContext;

希望此代码可以解决该错误。

I appreciate this question is old, but I had the exact same problem. To help the original poster and other users, I have altered the code from the original post to the following:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    SPSite sc = new SPSite(SPContext.Current.Site.ID);
    SPServiceContext context = SPServiceContext.GetContext(sc);

    HttpContext currentContext = HttpContext.Current;
    HttpContext.Current = null;

    UserProfileManager profileManager = new UserProfileManager(context);

    IEnumerator profileEnum = profileManager.GetEnumerator();

    while (profileEnum.MoveNext())
    {
        UserProfile up = (UserProfile)profileEnum.Current;

        if ((up["FirstName"] != null && up["FirstName"].Value != null && !String.IsNullOrEmpty(up["FirstName"].Value.ToString()))
            && (up.PublicUrl != null && !String.IsNullOrEmpty(up.PublicUrl.ToString())))
        {
            DataRow drUserProfile;

            drUserProfile = dtUserProfile.NewRow();

            drUserProfile["DisplayName"] = up.DisplayName;
            drUserProfile["FirstName"] = up["FirstName"].Value;
            drUserProfile["LastName"] = up["LastName"].Value;
            drUserProfile["Department"] = up["Department"].Value;
            drUserProfile["Location"] = up["SPS-Location"].Value;           

            drUserProfile["MySiteUrl"] = up.PublicUrl.ToString().Replace(@"\", @"\");

            dtUserProfile.Rows.Add(drUserProfile);
        }
    }
}

HttpContext.Current = currentContext;

Hopefully this code should resolve the error.

幸福不弃 2024-10-25 09:09:35

您是否尝试过 SPSite.SystemAccount.UserToken 或 SPWeb.AllUsers["user"].UserToken,而不是获取 SPSite.Owner 的 UserToken;

如果可能的话,我会做后者,最小特权规则等。

Instead of getting the UserToken of SPSite.Owner, have you tried SPSite.SystemAccount.UserToken, or SPWeb.AllUsers["user"].UserToken;

I'd do the latter if possible, rule of least privileges etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文