2014-12-19 157 views
2

我有一個應用程序部署在具有表單身份驗證模式的IIS 8上。 現在有一個需求,如果Windows用戶和應用程序用戶不同,現在業務需要顯示彈出消息並創建審計日誌。獲取Windows登錄用戶名ASP .net

爲此,我想在應用程序的logincontrol上獲取Windows登錄用戶。我嘗試了很多方法,但沒有任何幫助。

如果可能或不可以,請引導我。

傢伙人。請幫我...

+0

如何http://stackoverflow.com/問題/ 6615680/asp-net-getting-current-user-name(最後一篇文章)? – reporter 2014-12-19 10:17:51

+0

這可以幫助你[鏈接](http://stackoverflow.com/questions/5417125/how-to-get-current-user-whos-accessing-asp-net-app) – LPs 2014-12-19 10:20:25

+0

如果您使用表單身份驗證然後應用程序沒有Windows用戶的知識。理論上可能沒有,即使在實踐中也是如此。從Web應用程序的角度來看,用戶是表單驗證用戶,沒有Windows用戶。如果您使用Windows身份驗證,那麼它是微不足道的。 – 2014-12-19 10:22:21

回答

13

我用這個網站作爲一個參考:

http://richhewlett.com/2011/02/15/getting-a-users-username-in-asp-net/

(如果本站轉載總結下降)

方案1 :關閉模擬的IIS中的匿名身份驗證。

HttpContext.Current.Request.LogonUserIdentity.Name COMPUTER1\IUSR_COMPUTER1 
HttpContext.Current.Request.IsAuthenticated False 
HttpContext.Current.User.Identity.Name - 
System.Environment.UserName ASPNET 
Security.Principal.WindowsIdentity.GetCurrent().Name COMPUTER1\ASPNET 

場景2:IIS中的Windows身份驗證模擬關閉。

HttpContext.Current.Request.LogonUserIdentity.Name MYDOMAIN\USER1 
HttpContext.Current.Request.IsAuthenticated True 
HttpContext.Current.User.Identity.Name MYDOMAIN\USER1 
System.Environment.UserName ASPNET 
Security.Principal.WindowsIdentity.GetCurrent().Name COMPUTER1\ASPNET 

方案3:匿名身份驗證在IIS中,模擬對

HttpContext.Current.Request.LogonUserIdentity.Name COMPUTER1\IUSR_COMPUTER1 
HttpContext.Current.Request.IsAuthenticated False 
HttpContext.Current.User.Identity.Name - 
System.Environment.UserName IUSR_COMPUTER1 
Security.Principal.WindowsIdentity.GetCurrent().Name COMPUTER1\IUSR_COMPUTER1 

方案4:Windows身份驗證在IIS中,模擬對

HttpContext.Current.Request.LogonUserIdentity.Name MYDOMAIN\USER1 
HttpContext.Current.Request.IsAuthenticated True 
HttpContext.Current.User.Identity.Name MYDOMAIN\USER1 
System.Environment.UserName USER1 
Security.Principal.WindowsIdentity.GetCurrent().Name MYDOMAIN\USER1 
+0

OP正在使用表單驗證,所以這些都不會有幫助。 – 2014-12-19 10:23:18

+0

這些都不能幫助我。我也嘗試使用Request.ServerVariables [「LOGON_USER」],但它也返回空字符串,因爲在IIS中啓用了匿名身份驗證。如果我禁用匿名身份驗證,則表單身份驗證不起作用。 夥計們請幫幫我。 – 2014-12-19 10:37:03

+0

你是否使用MVC?因爲您可以指定哪些操作是匿名的,哪些不是基於此操作的。 – user3036342 2014-12-19 11:05:52