对外的门户网站往往会开启匿名访问,如果在门户网站上写关于权限的代码时一定要当心,如果不事先判断当前用户是否为匿名用户,有些方法就会直接报错,比如:
web.IsCurrentUserMemberOfGroup。
直接用 SPContext.Current.Web.CurrentUser 就可以判断当前用户的身份,如果返回null说明是匿名用户。
一个例子:
protected void Page_Load(object sender, EventArgs e)
{bool isOwner;
if (SPContext.Current.Web.CurrentUser !=null )
{SPWeb web = SPContext.Current.Web;
isOwner=web.IsCurrentUserMemberOfGroup(web.AssociatedOwnerGroup.ID);....
}
}