Use runwithelevatedprivileges to simulate the administrator privilege and use spcontext with caution.

Source: Internet
Author: User

In WSS 3.0, we use:

 
Spsecurity. runwithelevatedprivileges (Delegate(){// Implementation Details omitted});

Can be upgradedCodeTo simulate the Administrator identity.

Do not use spcontext in runwithelevatedprivileges. current. web, spcontext. current. site, spcontrol. getcontextweb (httpcontext. current) and so on. Get the current web or site based on the current context, and get all the objects based on these methods (including the list, listitem, and other objects based on these objects) all operations are based on the current website login user permissions. Even in runwithelevatedprivileges, their operation permissions are not administrator.

Therefore, if you want to allow the code in runwithelevatedprivileges to operate normally as an administrator, you must reinitialize the corresponding object, for example:

 
Spsecurity. runwithelevatedprivileges (Delegate(){Using(Spsite mysite =NewSpsite (spcontext. Current. Site. url) {response. Write (mysite. rootweb. currentuser. loginname );}});
 
The above mysite. rootweb. currentuser. loginname returns the Administrator's Logon account.
However, if spcontext is used as described earlier:
 
Spsecurity. runwithelevatedprivileges (Delegate() {Response. Write (spcontext. Current. Web. currentuser. loginname );});

At this time, even if the account is running within the range of elevation of permissions, the account name for logon to the current website is obtained, rather than the administrator account.

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.