2010-04-27 77 views
1

我在寫一個sharepoint web部分。它將日誌寫入文件(使用StreamWriter)。但是,日誌僅寫入其託管Web部件的服務器上的帳戶是管理員的用戶。我想要檢測哪個帳戶(可能不是通過使用SPUser)正在執行Web部件的代碼,以便我可以爲較少特權的用戶生成日誌。那可能嗎?如何檢測哪個Windows帳戶正在運行.net應用程序?

感謝

回答

1

這段代碼會爲你提供用戶所屬的組。

var id = System.Security.Principal.WindowsIdentity.GetCurrent(); 
IdentityReferenceCollection irc = id.Groups; 
foreach (IdentityReference ir in irc) 
    Console.WriteLine(ir.Value); 
1

有一些方法都到相同的結果:我覺得一個

  • Request.LogonUserIdentity
  • HttpContext.Request.LogonUserIdentity
  • Page.User.Identity

從那裏必須在SharePoint服務器上的作品。哪一項工作將取決於你想要包含代碼的上下文,你應該在你需要的地方放置一個斷點,並且在監視窗口中看到上面的一個構造。如果有人在那裏工作,您會收到System.Security.Principal.WindowsPrincipal類型的對象,它可以爲您提供所需的所有內容。例如,名稱屬性是用戶名。 Groups屬性是SecurityIdentifier的列表,它對應於用戶所屬的組。可以使用NTAccount類將SecurityIdentifier轉換爲名稱。 (參見例如,I have a SID of a user account, and I want the SIDs of the groups it belongs to

相關問題