如何在asp.net中识别impersonate ="true"时获取Windows用户名?

MrD*_*pan 45 asp.net-mvc impersonation

我正在创建一个内部网asp.net mvc应用程序,公司中的每个人都应该可以访问它.我需要运行模拟数据库访问的网站等,但我想知道每个用户是谁.

当我看着Page.User.Identity.Name它的空白时.即使网站正在模拟运行,是否可以获取用户的Windows帐户名称?

编辑: 这里有更多信息.我在IIS 6中有一个运行匿名访问的站点.该站点在可以访问数据库的系统帐户下运行(因为所有员工都无权访问数据库).

我的web.config有<authentication mode="Windows" /><identity impersonate="true"/>

我的目标是用户不必登录 - 他们登录我们网络的事实(以及该网站不在外部IP上的事实)是足够的身份验证.我想知道用户是谁以跟踪他们所做的更改等.

小智 121

随着<authentication mode="Windows"/>您的应用程序,并在IIS中启用匿名访问,您将看到以下结果:

System.Environment.UserName: Computer Name
Page.User.Identity.Name: Blank
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Computer Name 
Run Code Online (Sandbox Code Playgroud)

随着<authentication mode="Windows"/>在你的应用程序,而"匿名访问"禁用的,只有"集成Windows身份验证"在IIS中,您将看到以下结果:

System.Environment.UserName: ASPNET (user account used to run ASP.NET service)
Page.User.Identity.Name: Domain\ Windows Account Name 
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Computer Name\ASPNET
Run Code Online (Sandbox Code Playgroud)

随着<authentication mode="Windows"/><identity impersonate ="true"/>在你的应用程序,和"匿名访问"禁用的,只有"集成Windows身份验证"在IIS中,您将看到以下结果:

System.Environment.UserName: Windows Account Name 
Page.User.Identity.Name: Domain\ Windows Account Name 
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Domain\ Windows Account Name
Run Code Online (Sandbox Code Playgroud)

  • 这是其中一个答案,我希望有一个最喜欢的答案,如最喜欢的问题 (2认同)

Gav*_*vin 6

尝试这个

System.Security.Principal.WindowsIdentity.GetCurrent().Name
Run Code Online (Sandbox Code Playgroud)

它应该返回带有用户登录名的字符串

  • 想想您将需要在IIS中禁用匿名访问,因为当他们访问站点时假定他们已登录到域,IIS将使用其当前登录名。然后,上面的代码应显示其UserID,而不是在其下运行的IIS。 (2认同)