When we decide to create,update or delete items in sitecore programmatically we need to run our code in either Securitydisabler context or UserSwitcher context.
when we want to run the code in context of another user we should use Sitecore.Security.Accounts.UserSwitcher class for example there are scenarios in which only user A is allowed to create,update or delete items under a particular node in the tree and you want the code to fail if someone else tries to do the changes.
string userA = @”sitecore\userA”;
Sitecore.Security.Accounts.User sitecorecuser =Sitecore.Security.Accounts.User.FromName(userA, false);
using (new Sitecore.Security.Accounts.UserSwitcher(sitecorecuser))
{
//your code goes here
}
when we want to run the code without any security restrictions then we should use Sitecore.SecurityModel.SecurityDisabler class. Basically SecurityDisabler elevates the users permission (temporarily) to admin rights and so context user will be able to perform anything in sitecore. Any changes done with the SecurityDisabler will show up as being done by the sitecore\Anonymous role.
using (new Sitecore.SecurityModel.SecurityDisabler()) { //your goes code here }