摘自:
http://www.cnblogs.com/netcai/archive/2008/09/11/1288897.html
在编写WebPart或EventHandler时,当前登录网站的是一个只具有普通权限如查看而没有编辑添加权限的用户(如:yuana),而在这个网站中存在一个利用当前登录网站用户的权限去执行向某列表或文档库中添加新记录的WebPart里的代码,但是这时当前登录的用户yuana并没有相应的对列表或文档库添加新记录的权限,这时就要在WebPart的代码里做文章了,这就是我要提到的模拟提升当前用户的权限以更高的权限来运行代码。
//
模拟提升权限
SPSecurity.RunWithElevatedPrivileges(
delegate
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//创建指定站点集
using (SPSite site = new SPSite("http://crmg-ea/mdms"))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//打开网站
using (SPWeb web = site.OpenWeb())
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//允许更新网站
web.AllowUnsafeUpdates = true;
//打开所属的文档库
SPList list = web.Lists[ListName];
………….
web.AllowUnsafeUpdates = false;
}
}
}
);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
创建指定站点集
using
(SPSite site
=
new
SPSite(
"
http://crmg-ea/mdms
"
))
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//模拟提升权限
SPSecurity.RunWithElevatedPrivileges(delegate
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//打开网站
using (SPWeb web = site.OpenWeb())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//允许更新网站
web.AllowUnsafeUpdates = true;
//打开所属的文档库
SPList list = web.Lists[ListName];
………….
web.AllowUnsafeUpdates = false;
}
});
}
在编写WebPart或EventHandler时,当前登录网站的是一个只具有普通权限如查看而没有编辑添加权限的用户(如:yuana),而在这个网站中存在一个利用当前登录网站用户的权限去执行向某列表或文档库中添加新记录的WebPart里的代码,但是这时当前登录的用户yuana并没有相应的对列表或文档库添加新记录的权限,这时就要在WebPart的代码里做文章了,这就是我要提到的模拟提升当前用户的权限以更高的权限来运行代码。
在MOSS代码中用来提升权限,模拟成站点管理员的权限,在这过程需要用户MOSS里的对象模型里的SPSecurity.RunWithElevatedPrivileges来进行。SPSecurity.RunWithElevatedPrivileges对象是可以将当前用户在代码里的安全上下文凭据提升模拟成当前站点的管理员的权限运行此代码。
如第一种情况:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这种情况下创建的SPSite对象会以全新的身份就是以站点管理员(SPSAdmin)的身份来运行下面的方法体的,而不是当前登录用户真正在此网站里的权限。
如第二种情况:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这种情况那么所创建的用户安全上下文的权限就是以当前登录用户在此网站中真正实际权限来运行接下来的代码方法体,虽然代码中创建的SPWeb对象打开网站是在SPSecurity.RunWithElevatedPrivileges模拟权限的方法块中运行,但根据代码的运行结果可以看出SPSecurity.RunWithElevatedPrivileges对象并没有起到提升权限的效果。
这两种说明了在SPSecurity.RunWithElevatedPrivileges对象方法块中创建站点SPSite对象所产生的运行效果是完全不同的。
以下是自己遇到这种问题后,解决的实例:
private
DataTable GetListItems()
{
DataTable dt = new DataTable();
dt.Columns.Add( " title " , typeof ( string ));
dt.Columns.Add( " date " , typeof ( string ));
DataRow dr = null ;
DataColumn dc = null ;
int count = 0 ;
string url = string .Empty;
string linkName = string .Empty;
string linkFullName = string .Empty;
string tempItemUrl = string .Empty;
bool isDocument = false ;
// SPWebCollection webs = web.Webs;
string [] arrList = _listName.Split( ' , ' );
SPList listTemp = null ;
SPSecurity.RunWithElevatedPrivileges( delegate ()
{
foreach ( string str in arrList)
{
count = 0 ;
using (SPSite site = new SPSite(_siteUrl))
{
using (SPWeb web = site.AllWebs[_webName])
{
SPWebCollection webs = web.Webs;
for ( int i = 0 ; i < webs.Count; i ++ )
{
try
{
listTemp = webs[i].Lists[str];
}
catch
{
continue ;
}
if (listTemp != null )
{
isDocument = ((listTemp.BaseType == SPBaseType.DocumentLibrary) ? true : false );
foreach (SPListItem item in listTemp.GetItems(listTemp.DefaultView))
{
// _rowNumber + 1![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
.
if (count ++ > _rowNumber)
{
break ;
}
if (isDocument == false )
{
tempItemUrl = item.Url;
tempItemUrl = tempItemUrl.Substring( 0 , tempItemUrl.LastIndexOf( " / " ));
url = string .Format( " {0}/{1}/DispForm.aspx?ID={2} " , webs[i].Url, tempItemUrl, item.ID);
}
else
{
url = string .Format( " {0}/{1} " , webs[i].Url, item.Url);
}
dr = dt.NewRow();
// 1列
linkName = item[ " LinkTitle " ].ToString();
linkFullName = linkName;
if (linkName.Length > _stringLength)
{
linkName = linkName.Substring( 0 , _stringLength - 3 ) + "
"
;
}
dr[ 0 ] = string .Format( " ·<a href='{0}' title='{1}'>{2}</a> " ,
url, linkFullName, linkName);
// 2列
string result = string .Empty;
try
{
result = string .Format( " {0:yy-MM-dd} " , DateTime.Parse(item[ " _x4e8b__x4ef6__x65e5__x671f_ " ].ToString()));
}
catch
{
result = string .Empty; // item["_x4e8b__x4ef6__x65e5__x671f_"].ToString();
}
dr[ 1 ] = result;
dt.Rows.Add(dr);
}
}
}
}
}
}
});
dt.DefaultView.Sort = " date desc " ;
return dt;
}
{
DataTable dt = new DataTable();
dt.Columns.Add( " title " , typeof ( string ));
dt.Columns.Add( " date " , typeof ( string ));
DataRow dr = null ;
DataColumn dc = null ;
int count = 0 ;
string url = string .Empty;
string linkName = string .Empty;
string linkFullName = string .Empty;
string tempItemUrl = string .Empty;
bool isDocument = false ;
// SPWebCollection webs = web.Webs;
string [] arrList = _listName.Split( ' , ' );
SPList listTemp = null ;
SPSecurity.RunWithElevatedPrivileges( delegate ()
{
foreach ( string str in arrList)
{
count = 0 ;
using (SPSite site = new SPSite(_siteUrl))
{
using (SPWeb web = site.AllWebs[_webName])
{
SPWebCollection webs = web.Webs;
for ( int i = 0 ; i < webs.Count; i ++ )
{
try
{
listTemp = webs[i].Lists[str];
}
catch
{
continue ;
}
if (listTemp != null )
{
isDocument = ((listTemp.BaseType == SPBaseType.DocumentLibrary) ? true : false );
foreach (SPListItem item in listTemp.GetItems(listTemp.DefaultView))
{
// _rowNumber + 1
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
if (count ++ > _rowNumber)
{
break ;
}
if (isDocument == false )
{
tempItemUrl = item.Url;
tempItemUrl = tempItemUrl.Substring( 0 , tempItemUrl.LastIndexOf( " / " ));
url = string .Format( " {0}/{1}/DispForm.aspx?ID={2} " , webs[i].Url, tempItemUrl, item.ID);
}
else
{
url = string .Format( " {0}/{1} " , webs[i].Url, item.Url);
}
dr = dt.NewRow();
// 1列
linkName = item[ " LinkTitle " ].ToString();
linkFullName = linkName;
if (linkName.Length > _stringLength)
{
linkName = linkName.Substring( 0 , _stringLength - 3 ) + "
![](https://www.cnblogs.com/Images/dot.gif)
}
dr[ 0 ] = string .Format( " ·<a href='{0}' title='{1}'>{2}</a> " ,
url, linkFullName, linkName);
// 2列
string result = string .Empty;
try
{
result = string .Format( " {0:yy-MM-dd} " , DateTime.Parse(item[ " _x4e8b__x4ef6__x65e5__x671f_ " ].ToString()));
}
catch
{
result = string .Empty; // item["_x4e8b__x4ef6__x65e5__x671f_"].ToString();
}
dr[ 1 ] = result;
dt.Rows.Add(dr);
}
}
}
}
}
}
});
dt.DefaultView.Sort = " date desc " ;
return dt;
}