2009-01-05 30 views
5

我試圖找到我的SharePoint應用程序的當前用戶名。還有更多的方法如何做到這一點。但是,這種分享方式會帶來誤導性的價值。SPContext.Current.Web.CurrentUser返回誤導值

System.Security.Principal.WindowsIdentity.GetCurrent().Name // returns MY_COMPUTER\\my_user 

HttpContext.Current.User.Identity.Name // returns MY_COMPUTER\\my_user 

HttpContext.Current.Request.ServerVariables["AUTH_USER"] // returns MY_COMPUTER\\my_user 

Microsoft.SharePoint.SPContext.Current.Web.CurrentUser.LoginName // returns SHAREPOINT\\system 

這種行爲的原因是什麼?如果我使用非共享點方式,我會遇到問題嗎?

+0

這可能是一個設置問題,因爲粗略搜索互聯網似乎表明它正確返回,而不是您看到的SHAREPOINT \ System。 – 2009-01-05 16:34:40

回答

5

如果用戶是運行當前Web應用程序的應用程序池帳戶,則這是預期的。
BTW,它應該是爲顯示在歡迎控制(左上控制)

6

您是否以您用來安裝系統的管理員帳戶瀏覽? SharePoint將「幫助」重命名SHAREPOINT \ System。使用不同的帳戶,所有的方法將返回相同的值。

0

另一種方式SPWeb.CurrentUser可以返回SHAREPOINT相同的名稱\系統如果網站是高架,雖然我不知道爲什麼SPContext.Current會被提升。您在哪種頁面上看到這種行爲?

1

我想你可能已經包含在SPSecurity.RunWithElevatedPriviliges下的代碼。檢查一次。我不確定

4

問題是因爲您可能從RunWithElevatedPrivileges代碼中的高架SPWeb獲取當前用戶。您可以使用下面的代碼片段獲取真實用戶

SPWeb site = SPContext.Current.Web; 
SPSecurity.RunWithElevatedPrivileges(delegate() 
{ 
    using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID)) 
    { 
     using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID)) 
     { 
      string currUser = site.CurrentUser; //not the ElevatedSite.CurrentUser 
     } 
    } 
}); 

這將顯示真實的用戶名而不是SHAREPOINT \ System用戶。