When compiling a webpart or eventhandler, a user (for example, yuana) with only common permissions such as viewing and not editing and adding permissions is logged on to the website ), the website has a webpart that uses the permissions of the users currently logged on to the website to add new records to a list or document library. Code However, yuana, the currently logged-on user, does not have the permission to add new records to the list or document library. In this case, you need to do this in the webpart code. Article This is the simulation I want to mention to improve the current user's permissions to run the Code with higher permissions.
The moss code is used to improve the permissions and simulate the permissions of the site administrator. In this process, the spsecurity. runwithelevatedprivileges in the object model of the user Moss is required. The spsecurity. runwithelevatedprivileges object simulates the security context creden。 of the current user in the Code as administrator permissions of the current site to run this code.
For example, in the first case:
// Simulate permission escalation
Spsecurity. runwithelevatedprivileges (delegate
{
//Create a specified site set
Using (spsite site = new spsite ("http: // crmg-Ea/Mdms "))
{
// Open the website
Using (spweb web = site. openweb ())
{
// Allows website updates
Web. allowunsafeupdates = true;
// Open the document library
Splist list = web. Lists [listname];
.............
Web. allowunsafeupdates = false;
}
}
});
In this caseSpsiteThe object will be named as a site administrator.(Spsadmin)Instead of the permissions of the current logon user on this website.
For example, in the second case:
// Create a specified site set
Using (spsite site = new spsite ("http: // crmg-Ea/Mdms "))
{
// Simulate permission escalation
Spsecurity. runwithelevatedprivileges (delegate
{
// Open the website
Using (spweb web = site. openweb ())
{
// Allows website updates
Web. allowunsafeupdates = true;
// Open the document library
Splist list = web. Lists [listname];
.............
Web. allowunsafeupdates = false;
}
});
}
In this case, the created user security context permission is to run the following code method body based on the actual permissions of the currently logged-on user on this website, although the spweb object created in the Code opens the website in spsecurity. runwithelevatedprivileges runs in the method block where the permission is simulated, but spsecurity can be seen according to the code running result. the runwithelevatedprivileges object does not improve permissions.
The two methods are described inSpsecurity. runwithelevatedprivilegesSite creation in the object method BlockSpsiteThe Running Effect of the object is completely different.
[Original address]