首页 > 解决方案 > 如何获取 Windows 客户端用户 ASPX C#

问题描述

我在 StackOverflow 上查看并尝试了几十个“答案”,但没有一个有效。我有一个非常简单的 aspx 页面,后面有 C# 代码。该网站位于 Windows 2008R2 服务器上。该网站看起来像(实际名称已更改):

MyServer - set for Anonymous Authentication  
 Application Pools
      ASP.NET v4.0 Classic - .Net 4.0, Classic pipeline, App Pool Identity
      MySiteAppPool - .Net 2.0, Integrated, runs under a Domain-wide Service identity (call it "mycompany\domservice")
   Sites
      MyMainSite - Windows Authentication, uses "MySiteAppPool"
         "AutoPrint" - my web app, Windows Authentication, uses "ASP.Net v4.0 Classic" app pool, ASP.NET Impersonation enabled

我的“AutoPrint”网络应用程序有一个起始页“AutoPrint.aspx”和后面的代码(“AutoPrint.aspx.cs”,加上几个类)。

服务器和主站点不可更改,因为该站点下还有其他几个应用程序。

用户当前调用此应用程序:

http://MyServer/AutoPrint

Everything I have tried is returning the "mycompany\domservice" result:
Request.LogonUserIdentity.Name.ToString() - returns "mycompany\domservice"
System.Environment.UserName.ToString() - returns "domservice"
System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString() - returns "mycompany\domservice"

我在这里想念什么?为什么这么难?

进一步说明:“mycompany\domservice”-“domservice”帐户只是“mycompany”域中的一个 ActiveDirectory 帐户,它有权读取/写入站点和其他应用程序所需的目录。在安装站点和其他网络应用程序时,我们将该帐户用作“连接身份”用户。

我要做的是获取打开浏览器并访问此应用程序的人的 Windows 用户帐户的 ActiveDirectory 名称。如果用户“JJONES”登录 Windows 并使用“ http://myserver/autoprint ”启动应用程序,我想获取“JJONES”或“mycompany\JJONES”作为用户名。

标签: c#asp.net

解决方案


如果您使用匿名身份验证,则浏览器不会向服务器发送任何凭据(用户 ID/密码)。因此,如果您想要服务器上的客户端用户 ID,则必须使用非匿名身份验证,例如。窗口或窗体。您可以使用非匿名身份验证,然后允许或拒绝特定用户或用户组或所有用户访问您的网站。


推荐阅读