SharePoint v3:忘掉模擬用戶Impersonate,SPSecurity.RunWithElevatedPrivileges來了

回顧:

在SharePoint V2 大家應該都用過模擬用戶Impersonate這個功能,

這個功能用來暫時提升某個用戶的權限,比如某個普通用戶的本來不能修改某個列表的值,但是我們功能需要在修改。

缺點:

????我們使用這個模擬用戶功能時候,經常是明文保存用戶名密碼,是個安全隱患。

????更加氣憤的是,據我所知,在匿名用戶訪問狀態下面,根本不能夠模擬成功。

V3解決辦法:

Elevation of Privilege?

Elevation of privilege is a new feature of that enables you to programmatically perform actions in code using an increased level of privilege. The Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges method enables you to supply a delegate that runs a subset of code in the context of an account with higher privileges than the current user.

A standard usage of RunWithElevatedPrivileges is:

SPSecurity.RunWithElevatedPrivileges(delegate()

{

??? // do things assuming the permission of the "system account"

});

Frequently, to do anything useful within SharePoint you'll need to get a new SPSite object within this code to effect the changes.? For example:

SPSecurity.RunWithElevatedPrivileges(delegate()

{

??? using (SPSite site = new SPSite(web.Site.ID))

??? {

?????? // do things assuming the permission of the "system account"

????}

});

Although elevation of privilege provides a powerful new technique for managing security, it should be used with care. You should not expose direct, uncontrolled mechanisms for people with low privileges to circumvent the permissions granted to them.?

?

注意:

SPSite要在代碼塊里面創建,而不能使用當前的SPSite

// Uses the App poll creds with the SPUser's identity reference of user

SPSecurity.RunWithElevatedPrivileges(delegate()

{

// Gets a new security context using

using (SPSite site = new SPSite( SPContext.Current.Site.ID ))

{

using (SPWeb thisWeb = site.OpenWeb())

{

thisWeb.AllowUnsafeUpdates = true;

SPItem item = //web.GetListItem(this.Page.Request.Url.ToString());

thisWeb.GetList(ListName).GetItemById(ID);

item[FieldName] = (item[FieldName] == null) ? 1 : (double)item[FieldName] + 1;

item.Update();

?

writer.Write("Visited Counter. Current:(" + item[FieldName].ToString() + ")");

}

}

});

????運行那一段代碼的用戶是應用程序池的用戶,(在IIS里面設置,避免了明文保存)

????

????注意要關閉SPSite /SPWeb ,可以參考: http://msdn2.microsoft.com/en-us/library/aa973248.aspx

結束:

經過測試,匿名用戶也能成功。我的瀏覽計數功能就使用了該段代碼。

?

MSDN參考:

Elevation of Privilege : http://msdn2.microsoft.com/en-us/library/aa543467.aspx

Best Practices: Using Disposable Windows SharePoint Services Objects

轉自:http://www.cnblogs.com/cleo/archive/2007/04/06/sharepoint_v3_impersonate_spsecurity_runwithelevatedprivileges.html

轉載于:https://www.cnblogs.com/llbofchina/archive/2007/04/17/717065.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/536342.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536342.shtml
英文地址,請注明出處:http://en.pswp.cn/news/536342.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

UVA1225????????Digit Counting

Digit Counting UVA - 1225 題目傳送門 題目大意&#xff1a;輸入一個數字T&#xff0c;代表有T組測試數據&#xff0c;下面每行有一個整數n&#xff0c;求將1到n的數字連成一串后每個數字出現的個數。 AC代碼&#xff1a; #include <cstdio> #include <iostream&…

Chess Queen【數學】

Chess Queen UVA - 11538 題目傳送門 題目大意&#xff1a;輸入兩個整數n,m&#xff0c;在n行m列的棋盤中放入白黑兩個棋子&#xff0c;棋子在同一行、同一列或同一對角線上能相互進攻&#xff0c;問有多少種擺放方案。 AC代碼&#xff1a; #include <cstdio> #incl…

Java開發中保證接口的冪等性問題

目錄 1、解決方案 2、使用token保證接口冪等性的例子 3、在實際項目中&#xff0c;如何有效地使用token法來保證接口的冪等性&#xff1f; 4、3示例中如何獲取請求中的 token 5、如果token驗證失敗&#xff0c;如何處理 6、在上述示例代碼中加上token過期后重置的功能 7…

typedef 的四個用途和兩大陷阱

>>>>>用途一&#xff1a;定義一種類型的別名&#xff0c;而不只是簡單的宏替換。可以用作同時聲明指針型的多個對象。比如&#xff1a;char* pa, pb; // 這多數不符合我們的意圖&#xff0c;它只聲明了一個指向字符變量的指針&#xff0c; // 和一個字符變量&am…

Triangle Counting【數學】

Triangle Counting UVA - 11401 題目傳送門 題目大意&#xff1a;輸入一個整數n&#xff0c;求在1到n中選取三條邊能夠組成多少種三角形。 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #in…

Cheerleaders【容斥】

Cheerleaders UVA - 11806 題目傳送門 題目大意&#xff1a;給你三個整數n,m,k&#xff0c;代表有一個n行m列的場地&#xff0c;共有k個人&#xff0c;需保證在最外圍的一圈的每行每列都必須要有一個人&#xff0c;若這個人在對角上&#xff0c;則可以當做他所在的行列都已經…

Exploring Pyramids【動態規劃——區間DP】

Exploring Pyramids UVALive - 3516 題目傳送門 題目大意&#xff1a;給你一個字符串&#xff0c;其代表的是機器人來回走過的路徑&#xff0c;機器人總是先走左邊再走右邊&#xff0c;問有多少種情況。 解決方法&#xff1a;設輸入序列為S&#xff0c;d(i,j)為子序列Si,Si…

Investigating Div-Sum Property【數位DP】

Investigating Div-Sum Property UVA - 11361 題目傳送門 題目大意&#xff1a;輸入三個數a,b,k&#xff0c;問從a到b中有多少個數滿足數字能夠整除k&#xff0c;并且其數位和也能整除k。 解決方法&#xff1a;數位DP的模板題&#xff0c;Dp[x]表示在不超過x的數中滿足條件…

UVA - 455?Periodic Strings【字符串】

Periodic Strings UVA - 455 題目傳送門 題目大意&#xff1a;先輸入一個數字n&#xff0c;在輸入n行字符串&#xff0c;對每一個字符串輸出其最小的周期長度&#xff0c;每兩個輸出間有一空行。 AC代碼&#xff1a; #include <cstdio> #include <iostream> #…

螺旋方陣問題【數組】

輸入n&#xff0c;輸出n階螺旋方陣&#xff0c;下面為5階螺旋方陣&#xff1a;1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 下面為我的代碼&#xff1a; #include <cstdio> #include <iostream> #include &…

馬鞍點問題【數組】

如果在一矩陣中元素A[i][j]滿足A[i][j]為第i行的最小值&#xff0c;第j行的最大值&#xff0c;則稱這個元素為這個矩陣的馬鞍點&#xff0c;求m*n矩陣所有的馬鞍點。若需求一個矩陣的所有馬鞍點&#xff0c;其實只需將矩陣的每行的最小值與每列的最大值分別求出存在相應的數組中…

Ping pong【樹狀數組】

Ping pong UVALive - 4329 題目傳送門 題目大意&#xff1a;一條大街上住著n個乒乓球愛好者&#xff0c;經常組織比賽切磋技術。每個人都有一個不同的技能值ai。每場比賽需要三個人&#xff1a;兩名選手&#xff0c;一名裁判。他們有一個奇怪的規定&#xff0c;即裁判必須住…

Frequent values【線段樹】

Frequent values UVA - 11235 題目傳送門 題目大意&#xff1a;給出一個非降序的整數數組a1,a2,a3...an&#xff0c;你的任務是對一系列的詢問&#xff08;i,j&#xff09;&#xff0c;回答ai,ai1,ai2...aj中出現次數最多的值所出現的次數。輸入包括多組數據。每組數據第一行…

求二叉樹節點個數、葉子節點、節點層次與寬度

需實現&#xff1a;&#xff08;1&#xff09;輸出二叉樹b的節點個數 &#xff08;2&#xff09;輸出二叉樹b的葉子節點個數 &#xff08;3&#xff09;求二叉樹b中指定節點值&#xff08;假設所有節點值不同&#xff09;的節點的層次。 &#xff08;4&#xff09;利用層次遍歷…

UVA - 227?Puzzle

Puzzle UVA - 227 題目傳送門 注意點&#xff1a;每兩個輸出點間有一個換行&#xff0c;但最后一個輸出無換行 惡心模擬題&#xff0c;很卡輸入輸出&#xff01;&#xff01;&#xff01; AC代碼1:(自己的代碼&#xff0c;提交時需要選擇C11) #include <cstdio> #i…

UVA - 232????????Crossword Answers

Crossword Answers UVA - 232 題目傳送門 直接按照要求尋找遍歷一遍即可 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <m…

UVA - 1368????????DNA Consensus String

DNA Consensus String UVA - 1368 題目傳送門 解決方法&#xff1a;尋找每列中出現最多的字母。 AC代碼 #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #inc…

UVA - 202?Repeating Decimals

Repeating Decimals UVA - 202 題目傳送門 解決方法&#xff1a;模擬一下除法&#xff0c;及時記錄余數&#xff0c;當一個余數第二次出現時證明開始循環 AC代碼 #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #…

UVA - 10340????????All in All

All in All UVA - 10340 題目傳送門 將兩個字符串對比一下即可。 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <map> …

UVA - 1587????????Box

Box UVA - 1587 題目傳送門 解決方法&#xff1a;按照邊在12個長寬出現的次數和出現在幾個矩形里來判定就行了 總共出現一個長度&#xff0c;滿足條件 總共出現兩個長度&#xff0c;則其中一個長度在12個數里出現4次&#xff0c;并在四個矩形中出現 總共出現三個長度&#x…