如何检查用户是否有权访问 SharePoint 网站集

发布于 2024-10-02 18:32:19 字数 792 浏览 0 评论 0原文

检查用户是否有权访问网站集/网站的最佳方法是什么?我目前正在使用以下内容,

   SPSecurity.RunWithElevatedPrivileges(
       () => {using (var site = new SPSite(nodeUrl))
                     {
                         using (var web = site.OpenWeb())
                         {
                             retValue=
                                 web.DoesUserHavePermissions(
                                     context.User.Identity.Name,
                                     SPBasePermissions.Open);
                         }
                     }
             });

这似乎无法正常工作。如果用户从未添加到站点,则此方法有效。但是,如果添加用户然后删除,DoesUserHavePermission(.. SPBasePermission.Open) 仍返回 true,但当用户尝试访问网站时,SharePoint 会抛出访问被拒绝页面。

经过进一步挖掘,我发现用户帐户仍在 web.AllUsers 列表中,但没有分配角色。

What is the best way to check if a user has permission to a site collection/site? I'm currently using the following

   SPSecurity.RunWithElevatedPrivileges(
       () => {using (var site = new SPSite(nodeUrl))
                     {
                         using (var web = site.OpenWeb())
                         {
                             retValue=
                                 web.DoesUserHavePermissions(
                                     context.User.Identity.Name,
                                     SPBasePermissions.Open);
                         }
                     }
             });

This doesn't seem to be working properly. If the user was never added to the site this works. But if the user was added and then removed DoesUserHavePermission(.. SPBasePermission.Open) still returns true, but when the user tries to access the site SharePoint throws the access denied page.

After a little more digging I found that the user account is still in the web.AllUsers list, but it has no Roles assigned.

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

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

发布评论

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

评论(3

给不了的爱 2024-10-09 18:32:19

使用 CheckPermissions 而不是 DoesUserHavePermissions。请参阅 SPWeb.CheckPermissions 方法

Use CheckPermissions instead of DoesUserHavePermissions. See the SPWeb.CheckPermissions Method
.

终弃我 2024-10-09 18:32:19

我执行类似的检查(循环检查权限的工作区列表),这是我使用的相关代码:

string LoginName = SPContext.Current.Web.CurrentUser.LoginName
bool permission = web.DoesUserHavePermissions(LoginName, SPBasePermissions.Open)

I perform a similar check (looping over a list of workspaces checking for permissions), here is the relevant bit of code I use:

string LoginName = SPContext.Current.Web.CurrentUser.LoginName
bool permission = web.DoesUserHavePermissions(LoginName, SPBasePermissions.Open)
苯莒 2024-10-09 18:32:19

我认为您走在正确的道路上

SP 用户是 SiteCollection(站点)的成员,而不是特定的 Web 成员。

您还需要从代码中再次检查 Site.RootWeb

,我认为您没有获得实际的上下文

SPContext.Current 将是正确的上下文

 SPSecurity.RunWithElevatedPrivileges(
   () => {using (var site = new SPSite(nodeUrl))
                 {
                     using (var web = site.OpenWeb())
                     {
                         retValue=
                             web.DoesUserHavePermissions(
                                 context.User.Identity.Name,
                                 SPBasePermissions.Open);
                     }
                 }
         });

祝你好运

I think you are on the right track

The SP User is member of the SiteCollection (Site) and not the Web in particular.

you need to check agains the Site.RootWeb

also from your code, I think you don't get the actual Context

SPContext.Current would be the correct context

 SPSecurity.RunWithElevatedPrivileges(
   () => {using (var site = new SPSite(nodeUrl))
                 {
                     using (var web = site.OpenWeb())
                     {
                         retValue=
                             web.DoesUserHavePermissions(
                                 context.User.Identity.Name,
                                 SPBasePermissions.Open);
                     }
                 }
         });

Best of luck

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