While working with share point there are several situations that we need to do impersonation.For example, while you work with site creation programmatically and you don't want to give full privilege to the user who does the same.In such Scenarios this small code be helpful to you

Before starting you have to use the below two name spaces

using System.Security.Permissions;

using System.Security.Principal;


Add the below two functions

UseAppPoolIdentity()

ReturnToImpersonatingCurrentUser()



First.JPG




Second.JPG

Please call the function UseAppPoolIdentity() where ever you want to impersonate the user with admin rights. When you finished with admin privileged operation just call t he second function ReturnToImpersonatingCurrentUser().