研究java技術(shù)
SPSecurity.RunWithElevatedPrivileges(delegate() { // implementation details omitted });
可以提升代碼的運(yùn)行權(quán)限,實(shí)現(xiàn)模擬管理員身份的功能。
在RunWithElevatedPrivileges中不要使用 SPContext.Current.Web,SPContext.Current.Site,SPControl.GetContextWeb(HttpContext.Current) 之類的根據(jù)當(dāng)前上下文得到當(dāng)前的Web或者Site,根據(jù)這些方法得到的所有對(duì)象(包括從根據(jù)這些對(duì)象得到的List,ListItem等等對(duì)象)都是以 當(dāng)前網(wǎng)站登錄用戶權(quán)限運(yùn)作的,即使是在RunWithElevatedPrivileges其運(yùn)作權(quán)限也不會(huì)是管理員。
所以,如果要真正讓在RunWithElevatedPrivileges中的代碼以管理員權(quán)限正常運(yùn)作的話,必須重新初始化相應(yīng)的對(duì)象,比如:
SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite mySite = new SPSite(SPContext.Current.Site.Url)) { Response.Write(mySite.RootWeb.CurrentUser.LoginName); } });
以上mySite.RootWeb.CurrentUser.LoginName返回的是管理員的登錄帳號(hào)。
但是如果按之前所說使用SPContext:
SPSecurity.RunWithElevatedPrivileges(delegate() { Response.Write(SPContext.Current.Web.CurrentUser.LoginName); });
這時(shí)候即使在提升權(quán)限的范圍內(nèi)運(yùn)行,得到的也是當(dāng)前網(wǎng)站登錄帳戶名,而不是管理員登錄帳號(hào)
Powered by: BlogJava Copyright © 蜘蛛