2014-02-05 32 views
0

我使用impersonate在我的網站,我需要記錄每個請求到網站,所以也需要用戶名,我想我們可以得到這個服務器變量,但Auth_UserLogOn_User都返回空字符串。在impersonate之前和impersonate之後,如何獲取用戶名?有沒有人有任何想法?獲取IIS用戶之前模擬和模擬在ASP.NET後面代碼

回答

0

使用HttpContext.Current.User.Identity

WindowsIdentity wId = (WindowsIdentity)HttpContext.Current.User.Identity; 
WindowsIdentity wIdb4 = WindowsIdentity.GetCurrent(); 
string name = wIdb4.Name; 
Response.Write("Before impersonation"+name +"<br>");// <-- Writes ASPNET Account 


//Till this line,code is executed in the context of worker process 
WindowsImpersonationContext wIdCon = wId.Impersonate(); 
WindowsIdentity wIdafter = WindowsIdentity.GetCurrent(); 
name = wIdafter.Name; 
Response.Write("After Impersonation " + name + "<br>");// <-- writes Logged in user 

//Run in the context of logged authenticated user, do your //operations that require impersonation 

wIdCon.Undo(); 
WindowsIdentity wIdafterUndo = WindowsIdentity.GetCurrent(); 
name = wIdafterUndo.Name; 

Response.Write("After undo Impersonation " + name + "<br>"); 

OUTPUT 
Before impersonation SERVER\ASPNET 
After Impersonation TestAccount 
After undo Impersonation SERVER\ASPNET