Scenario:
You might want to impersonate a specific WSS user identity before creating a new object so that WSS user is recognized as the owner of the new object.
In order to impersonate a WSS user identity, you must first create an SPUserToken object. You can do this by accessing the UserToken property of an SPUser object. Once you have the SPUserToken object, you can use it to create a new SPSite object using an overloaded version of the SPSite class constructor.
Solution:
This is a very good alternate to RunWithElevatedPrivileges.
Code:
1. SPWeb siteCollection = SPContext.Current.Site;
2. SPWeb site = SPContext.Current.Web;
3.
4. // get SPUser object and acquire token
5. SPUser targetUser = site.SiteUsers[@"LITWAREINC\BrianC"];
6. SPUserToken token = targetUser.UserToken;
7.
8. // create new SPSite and SPWeb object to impersonate user
9. using (SPSite impersonatedSiteCollection =
10. new SPSite(siteCollection.ID, token)) {
11. using (SPWeb impersonatedSite =
12. impersonatedSiteCollection.OpenWeb(site.ID)) {
13. // WSS identity switched to impersonate BrianC
14. // Windows identity does not change
15. }
16. }
Article: http://msdn.microsoft.com/en-us/magazine/cc163287.aspx
You might want to impersonate a specific WSS user identity before creating a new object so that WSS user is recognized as the owner of the new object.
In order to impersonate a WSS user identity, you must first create an SPUserToken object. You can do this by accessing the UserToken property of an SPUser object. Once you have the SPUserToken object, you can use it to create a new SPSite object using an overloaded version of the SPSite class constructor.
Solution:
This is a very good alternate to RunWithElevatedPrivileges.
Code:
1. SPWeb siteCollection = SPContext.Current.Site;
2. SPWeb site = SPContext.Current.Web;
3.
4. // get SPUser object and acquire token
5. SPUser targetUser = site.SiteUsers[@"LITWAREINC\BrianC"];
6. SPUserToken token = targetUser.UserToken;
7.
8. // create new SPSite and SPWeb object to impersonate user
9. using (SPSite impersonatedSiteCollection =
10. new SPSite(siteCollection.ID, token)) {
11. using (SPWeb impersonatedSite =
12. impersonatedSiteCollection.OpenWeb(site.ID)) {
13. // WSS identity switched to impersonate BrianC
14. // Windows identity does not change
15. }
16. }
Article: http://msdn.microsoft.com/en-us/magazine/cc163287.aspx
Comments
Post a Comment