[SharePoint]如何防止从代码跳到“拒绝访问”页面
2008-10-23 10:34
483 查看
在代码中执行没有权限的操作时,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)
作者:黎波
博客:http://bobli.cnblogs.com/
日期:2008年10月23日
当发生“拒绝访问”异常时,默认情况下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)
作者:黎波
博客:http://bobli.cnblogs.com/
日期:2008年10月23日
相关文章推荐
- Sharepoint如何自定义“拒绝访问”页面
- [急]请教:如何防止同时访问相同页面?
- SpringBoot项目如何访问jsp页面的示例代码
- 让代码在SharePoint页面执行&&如何在aspx页面中写代码
- 使用代码为SharePoint页面上的WebParts设置访问群体
- 让代码在SharePoint页面执行&&如何在aspx页面中写代码
- [PHP]如何防止用户从地址栏直接访问后台页面
- 如何防止通过IP地址访问Tomcat管理页面
- 如何在HADF的JAVA代码中访问资源文件中的值
- JavaScript从父页面获取子页面的值(子页面又如何访问父页面)
- ASP.NET中防止页面多次提交的代码实现
- 1-100之间的所有素数的和如何在JSP页面实现 代码如下
- 如何禁止掉SharePoint页面个性化?
- 解决SharePoint 2010拒绝访问爬网内容源错误
- 提高代码质量及字节码如何防止内存错误
- 图片自动按比例缩小代码(防止页面被图片撑破)
- Jsp中如何防止页面被缓存
- WEB-INF下的jsp页面用户不能直接访问如何解决?
- (转)如何防止代码腐烂
- ASP.NET如何防止页面重复提交