[Original] a question about spsecurity. runwithelevatedprivileges [a problem about spsecurity. runwithelevatedprivileges

Source: Internet
Author: User

For spsecurity. runwithelevatedprivileges, there are manyArticleIt is used to execute privileges.CodeBut I have always reported permission exceptions in today's calls, so I need to take a closer look at it and pay attention to the following two issues:

1. spsecurity. runwithelevatedprivileges must be executed in a new context. This means that a new spsite and spweb are required. For example, the error code is as follows:

 
Spsecurity. runwithelevatedprivileges (Delegate() {Splist list = spcontext. Current. Web. list ["List name"] ;});

At this time, the running context is still the current user (for example, an anonymous user is not logged on), and its privileges are not elevated. Therefore, the following code is required to run the program correctly.

 spsecurity. runwithelevatedprivileges ( DeleGate  () {guid siteid = spcontext. current. web. site. ID;  using  (spsite impersonatedsite =  New  spsite (siteid )) { using  (spweb = impersonatedsite. rootweb) {splist list = web. list [ "list name" ] ;}}); 

 

2. essence of spsecurity. runwithelevatedprivileges

Spsecurity. runwithelevatedprivileges uses the system account Account to execute code.ProgramThe user in the associated application pool to execute the code.

in this image, the code is executed using the dev \ administrator account. Now there is a problem: if Dev \ administrator is not a website set user, or is it not the management of the website set? The answer is: exceptions may be reported due to insufficient permissions. Therefore, you must configure the identified account as the account corresponding to the system account of the website set or an administrator account. Similarly, if you want to call the corresponding attributes or methods in the spfarm instance in the code, pay attention to whether the user configured in the application pool is the server farm administrator, otherwise, a permission exception is reported.

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.