Download it:
https://sites.google.com/site/sasivalipireddy/home/RunWithElevatedPrivileages_ALL.pdf
- RunWithElevatedPrivileges?
- Why can’t we use RunWithElevatedPrivileges in event handlers?
- Impersonation Improvements in SharePoint 2010 Event Receivers?
- Best recommended practice use of it?
- Best recommended practice to use of it in Event Receivers?
- Best recommended practice to use of it in Feature Receivers?
- RunWithElevatedPrivileges in visual studio workflows:
- Is RunWithElevatedPrivileges allowed in sandbox solution?
- By using which credentials the RunWithElevatedPrivileges will run?
- Difference between SPSecurity.CodeToRunElevated and SPSecurity. RunWithElevatedPrivileges?
- RunWithElevatedPrivileges to impersonate as System Account user
- Passing User Token inside SPSite to impersonate as particular user
- Using Windows API
var site = new SPSite(SPContext.Current.Site.ID, SPContext.Current.Site.SystemAccount.UserToken);
Difference between RunWithElevatedPrivileges Vs SPUserToken:
- RunWithElevatedPrivileges to impersonate as System Account user
- Passing User Token inside SPSite to impersonate as particular user
SPSecurity.RunWithElevatedPrieveleges, there are too much tricks that you should take care of.
For Instance: You must create the new SPSite objects inside the delegate because SPSite objects created outside do not have Full Control even when referenced inside the delegate.
RunWithElevatedPrivileges does not work when HTTPContext is null:
RunWithElevatedPrivileges don’t work when HTTPContext (SPContext to be more specific) is null. So, you will not have elevation of privilege when using RunWithElevatedPrivileges in Console Application, Workflow, Timer Job or Event handlers not initiated by a request in browser.
If you use instances of SPSite or SPWeb, obtained prior to the RunWithElevatedPrivileges block, it won't work as expected because they are already associated to a non-elevated security context[ means current logger user]
Why can’t we use SPContext.Current.Web inside RunWithElevatedPrivileges:
Error : Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
To address the issue, a new instance of SPSite and SPWeb should be cerated within the RunWithElevatedPrivileges code block as above.
Recommended practice #1:
private void Test()
{
Guid webID = SPContext.Current.Web.ID;
Guid siteID = SPContext.Current.Site.ID;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteID))
{
using (SPWeb web = site.OpenWeb(webID))
{
// Code Using the SPWeb Object goes here
}
}
});
}
Best recommended practice #2:
private void Test()
{
SPSite site = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite CurrentSite = new SPSite(site.ID))
{
using (SPWeb CurrentWeb = CurrentSite.OpenWeb(web.ID))
{
// Code Using the SPWeb Object goes here
}
}
});
}
RunWithElevatedPrivileges” in Feature Receivers:
[Guid("b321499d-9b43-410e-8a8f-779ffb81d738")]
public class Feature1EventReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
using (SPSite spSite = properties.Feature.Parent as SPSite)
{
using (SPWeb spWeb = spSite.OpenWeb())
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
//code here
});
}
}
}
catch (Exception ex)
{
}
}
Best Recommended Practice #1
namespace MyCustomDlgFramework.EventReceiver
{
/// <SUMMARY>
/// List Item Events
/// </SUMMARY>
public class EventReceiver : SPItemEventReceiver
{
/// <SUMMARY>
/// An item is being added.
/// </SUMMARY>
public override void ItemAdding(SPItemEventProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
{
//code here
}
}
});
}
}
}
Best recommended practice #2:
namespace MyCustomDlgFramework.MyEventReceiver
{
/// <SUMMARY>
/// List Item Events
/// </SUMMARY>
public class MyEventReceiver : SPItemEventReceiver
{
/// <SUMMARY>
/// An item is being added.
/// </SUMMARY>
public override void ItemAdding(SPItemEventProperties properties)
{
using (SPSite site = new SPSite(properties.WebUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
//Code here
});
}
}
}
}
}
Best recommended practice #3:
namespace MyCustomDlgFramework.EventReceiver
{
/// <SUMMARY>
/// List Item Events
/// </SUMMARY>
public class EventReceiver : SPItemEventReceiver
{
/// <SUMMARY>
/// An item is being added.
/// </SUMMARY>
public override void ItemAdding(SPItemEventProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPWeb web = properties.OpenWeb())
{
//Code here
}
});
}
}
}
Chances of getting “Access Dined” error:
Note: Elevation of privilege occurs only if newSPSite created inside the block :
Reason: SPContext.Current.Site and SPContext.Current.Web runs the List Item update code in the context of the currently logged in user and not in the context of the App Pool identity
private void Test()
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb currentWeb = SPContext.Current.Web;
SPList spList = currentWeb.Lists["MyList"];
});
}
private void Test()
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite currentSite = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb currentWeb = currentSite.OpenWeb())
{
// Access granted as System account!!
}
}
});
}
private void Test()
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["MyList"];
SPListItem item = list.GetItemById(1);
item["MyField"] = "SharePoint";
item.Update();
web.AllowUnsafeUpdates = false;
});
}
本文探讨了在SharePoint中使用RunWithElevatedPrivileges方法进行权限提升的最佳实践,包括在不同场景如事件接收器、特性接收器中的应用技巧。同时介绍了如何正确使用SPUserToken实现用户身份冒充,并对比了不同方法之间的区别。
1565

被折叠的 条评论
为什么被折叠?



