嗯……迟到总比没有好。我今天遇到了同样的问题。先前的评论没有解决原始发帖人表达的问题。当您在 _Layouts 文件夹中发布 .ASPX 页面时,问题就出现了,然后,当使用 Forms 或 Claims auth 时,将该自定义页面设为您在会话中的第一次点击(之前没有记住登录)。默认情况下不会触发 SharePoint 身份验证(即使您从 LayoutsPageBase 类继承)。如果您导航到其他一些 SharePoint 页面(例如 _Layouts/15/Settings.aspx)然后返回,则会填充 CurrentUser 。我必须使用 Reflector 来更好地了解发生了什么以及如何修理它。简短的回答是,一旦你意识到 CurrentUser == null,你需要添加这行代码:
Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(new UnauthorizedAccessException());
在我的例子中,这段代码生成了一个对我用来登录的浏览器的质询/响应,并且在这行代码之后,CurrentUser 对象被正确填写。这是我的整个函数最终的样子:
public static bool isAdminAuthorized()
{
Microsoft.SharePoint.SPContext oContext ;
Microsoft.SharePoint.SPWeb oWeb ;
Microsoft.SharePoint.SPUser oUser ;
try
{
oContext = Microsoft.SharePoint.SPContext.Current;
}
catch { throw new Exception("Can't obtain Sharepoint Context!"); }
try
{
oWeb = oContext.Web;
}
catch { throw new Exception("Can't obtain Sharepoint web!"); }
try
{
oUser = oWeb.CurrentUser;
}
catch { throw new Exception("Can't obtain Sharepoint current user!"); }
if (oUser == null)
{
Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(new UnauthorizedAccessException());
oUser = oWeb.CurrentUser;
}
foreach (Microsoft.SharePoint.SPGroup oGroup in oUser.Groups)
{
if (oGroup.Name.ToUpper().Contains("OWNER"))
{
return true;
}
}
return false;
}