我需要检查特定用户是否可以通过编程方式访问 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;
}
}
}
}
}
但出现错误
bool check = thirdlevel.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open);
作为 :
访问被拒绝。(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))