Upgrade the code running permission of the current user in Moss

Source: Internet
Author: User
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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.