Microsoft  CSIP   WindowsMobile   MEDC   合作伙伴   体验中心 设为首页 加到收藏夹

 
登录       点击换一张     注册新用户 找回密码

[SharePoint]如何防止从代码跳到“拒绝访问”页面
2008年11月18日03:32   编辑:黎波 来源: 博客园 浏览: 151

在代码中执行没有权限的操作时,SharePoint平台会抛出“拒绝访问(access denied)”异常,并将用户重定向到_layouts/AccessDenied.aspx页面。即使你尝试使用try...catch...finally来捕获处理这个异常,但是代码会在出错的地方停止继续执行,并且异常并不能被你的代码catch住。

当发生“拒绝访问”异常时,默认情况下SharePoint平台会自己捕获它,并将用户重定向到_layouts/AccessDenied.aspx页面,以便告诉用户没有权限,可以请求管理员给自己分配权限,或使用一个有权限的用户登陆。

如果你想在代码中处理这个异常,需要将Microsoft.SharePoint.SPSecurity.CatchAccessDeniedException属性设置为false,这样SharePoint平台就不会自动处理这个异常。下面是一段实例代码:

// for the assignment site, if the user doesn't have permission to view it
// we'll catch the exception 

bool previousValue = SPSecurity.CatchAccessDeniedException;
SPSecurity.CatchAccessDeniedException 
= false
;
try

{
    
using (SPSite spSite = new SPSite(strWebUrl))
    {
        
using (SPWeb spWeb =
 spSite.OpenWeb())
        {
            
//do some operations. An access denied exception may be thrown from here.

        }
    }
}
catch (UnauthorizedAccessException)
{
    
//do nothing. Catch the exception to not to restrict the user to access the content if user does not have access to SPWeb.

}
catch
 (FileNotFoundException)
{
    
// do nothing . Catch the exception to not to restrict the user to access the content if SPWeb does not exist.

}
finally

{
    SPSecurity.CatchAccessDeniedException 
= previousValue;
}

使用SPSecurity.CatchAccessDeniedException来防止跳到“拒绝访问”页面是官方推荐的方法,还有另外一个方法是设置Microsoft.SharePoint.SPSite.CatchAccessDeniedException属性,这个属性是SharePoint保留给自己内部使用的,不建议在我们的开发代码中使用。

参考:
How to avoid Access denied page
SPSecurity.CatchAccessDeniedException Property (Microsoft.SharePoint)

SPSite.CatchAccessDeniedException Property (Microsoft.SharePoint)

发表评论
评论标题 :
评论内容 :

    查看评论

请您注意:遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。 本站拥有管理笔名和留言的一切权利。
相关文章推荐
· [SharePoint]如何在Custom Action定义中包含当前页的URL 2008-11-18  黎波
· ASP.NET 2.0高级数据处理之处理Null值 2008-5-13  陶刚编译
· ASP.NET 2.0高级数据处理之主从数据表 2008-5-13  陶刚编译
· ASP.NET 2.0高级数据处理之使用参数 2008-5-13  陶刚编译
· 细节决定成败 ASP.NET中的蝴蝶效应 2008-5-13  马甲走江湖

  

论坛推荐  
热点活动
更多»

© CSIP 信息产业部软件与集成电路促进中心 All Rights Reserved 版权所有 京ICP备06020771号
联系电话:010-63951881-8003 王先生        邮件:wangj@csip.org.cn