SharePoint 中特定网站的用户权限检查

发布于 2024-11-09 22:43:23 字数 1729 浏览 5 评论 0原文

我需要检查特定用户是否有权以编程方式访问 SharePoint 中的网站/子网站。

注意:登录用户可以访问我的 SharePoint 网站中的几个网站。所以我必须只显示用户有权访问的网站。

我使用:

   using (SPWeb web = new SPSite(url).OpenWeb())
                {
                    SPWebCollection Sites = web.Webs;

                    foreach (SPWeb website in Sites)
                    {
                        SPUser loginUser = website.CurrentUser;
                        string username = loginUser.Name;
                        if (!website.IsRootWeb)
                        {
                            SPWebCollection subsites = website.Webs;
                            foreach (SPWeb supersubsite in subsites)
                            {

                                SPWebCollection thirdlevelsites = supersubsite.Webs;
                                foreach (SPWeb thirdlevel in thirdlevelsites)
                                {

thirdlevel.Site.CatchAccessDeniedException = false;
                                bool check = thirdlevel.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open);
                                if (check)
                                {
                                }
                                thirdlevel.Site.CatchAccessDeniedException = true;                                   
                                }

                            }
                        }
                    }
                }

但在 as 处出现错误

bool check = thirdlevel.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open);

访问被拒绝。 (HRESULT 异常:0x80070005 (E_ACCESSDENIED))

I need to check whether a particular user has access to a site/subsite in SharePoint programmatically.

Note: login user can have acces to few sites in my SharePoint site. So I have to show sites only to which the user have permission.

I used:

   using (SPWeb web = new SPSite(url).OpenWeb())
                {
                    SPWebCollection Sites = web.Webs;

                    foreach (SPWeb website in Sites)
                    {
                        SPUser loginUser = website.CurrentUser;
                        string username = loginUser.Name;
                        if (!website.IsRootWeb)
                        {
                            SPWebCollection subsites = website.Webs;
                            foreach (SPWeb supersubsite in subsites)
                            {

                                SPWebCollection thirdlevelsites = supersubsite.Webs;
                                foreach (SPWeb thirdlevel in thirdlevelsites)
                                {

thirdlevel.Site.CatchAccessDeniedException = false;
                                bool check = thirdlevel.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open);
                                if (check)
                                {
                                }
                                thirdlevel.Site.CatchAccessDeniedException = true;                                   
                                }

                            }
                        }
                    }
                }

but getting error at

bool check = thirdlevel.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open);

as :

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

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

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

发布评论

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

评论(1

枕花眠 2024-11-16 22:43:24

如果您使用的是发布网站,则应依赖 PortalSiteMapProviderCombinedNavSiteMapProviderCurrentNavSiteMapProviderGlobalNavSiteMapProviderWebSiteMapProvider 等,具体取决于您的导航需求),因为这已经经过完全安全的修剪,您将能够以最佳性能遍历整个树。

否则,您应该在 SPWeb 对象上使用 GetSubwebsForCurrentUser(),此方法将仅返回当前用户可访问的子网站。

希望它会有所帮助。

If you're working with a Publishing Site, you should rely on the PortalSiteMapProvider (CombinedNavSiteMapProvider, CurrentNavSiteMapProvider, GlobalNavSiteMapProvider, WebSiteMapProvider, etc depending on your navigation needs) as this one is already fully security wise trimmed and you'll be able to traverse the complete tree with the best performance.

Otherwise, you should use the GetSubwebsForCurrentUser() on your SPWeb object, this method will only return sub web(s) accessible for the current user.

Hope it will help.

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