Personalize yourself as another user inside web code

I use sharepoint lists as a database. I want to somehow impersonate myself as another user in the webpart code and, as this user, I will have write and edit rights to the list.

My goal is to have full access only through the website code.

I am using MOSS 2007.

+3
source share
2 answers
+3
source

SPSecurity.RunWithElevatedPrivilieges() , , , , . , , , , , ( SharePoint 2007 SP 1). , .

UserToken , :

        SPUserToken userToken = null;
        SPSecurity.RunWithElevatedPrivileges(() =>
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                {
                    userToken = web.AllUsers["domain\\username"].UserToken;
                }
            }
        });

\username Windows. SPSite :

        using (SPSite site = new SPSite(SPContext.Current.Site.ID, userToken))
        {
            using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
            {
                // This code will execute under the credentials of the userToken user
            }
        }

, .

+11

Source: https://habr.com/ru/post/1795682/


All Articles