c# - 当 Web 应用程序以管理员身份运行时,如何在 ASP.NET Core 2.2 中获取当前 Windows 用户名?
问题描述
我们有一个 ASP.NET Core 2.2 Web 应用程序,托管在 IIS 上并在MyComputer\MyAdminUser Windows 用户(具有管理员权限)下运行。我需要获取正在使用我们的应用程序 f.ex 的当前 Windows 用户的名称。MyComputer\MyClientUser。所有客户端用户都是本地用户(MyComputer\ ...)。我该怎么做?
我尝试使用不同的方法,但没有人给我带来预期的结果(MyClientUser):
1. HttpContext.Current.Request.LogonUserIdentity;
“HttpContext”不包含“ Current
”的定义,并且找不到接受“HttpContext”类型的第一个参数的可访问扩展方法“Current”...
2. Request.LogonUserIdentity;
同样的问题LogonUserIdentity
3. Request.ServerVariables["LOGON_USER"];
同样的问题ServerVariables
4. Request.Headers
此列表不包含有关当前 Windows 用户的任何信息
5. Environment.UserName
结果是“我的管理员用户”
6. WindowsIdentity.GetCurrent().Name
结果是“MyComputer\MyAdminUser”
7. ManagementObjectCollection collection =
new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem")
.Get();
collection?.Cast<ManagementBaseObject>()?.First()?.Properties?["UserName"]?.Value
对于 Visual Studio 结果是正确的(当前 Windows 用户),但在 IIS 上Properties?["UserName"]?.Value
为 null 或空 ( Properties?["UserName"]?.Name
= "UserName"
)
我做错了什么,我怎样才能得到预期的名字?
解决方案
推荐阅读
- ios - Flutter将flutter ios内购验证所需的数据发送到服务端
- composer-php - Composer 和本地依赖项
- hugo - 自定义页面作为部分的索引,并在任何地方列出内容
- javascript - 当使用图像扩展名重命名非图像文件时,如何验证文件是 JavaScript 中的图像?
- java - com.example.wifiscanner W/System.err:引起:android.system.ErrnoException:打开失败:EACCES(权限被拒绝)
- javascript - 画廊过滤器,每个类别有几张照片
- r - 如何最有效地在 R 中创建邻接矩阵?
- xslt - 我正在尝试使用转换将值更新为路径。需要输入
- spring-boot - 执行:此发布者不支持多个订阅者(spring data r2dbc + oracle-r2dbc)
- swiftui - 在 SwiftUI 中调用 ObservableObject 的正确方法