c# - 如何在 ASP.net 中获取客户机用户登录?

标签 c# asp.net iis local

Localhost 上调用此代码时我的用户名是“MTA”:

string opl = HttpContext.Current.User.Identity.Name.ToString();
TextBox1.Text = opl.Substring(opl.IndexOf(@"\") + 1);

或此代码:

string opl = System.Environment.UserName.ToString();
TextBox1.Text = opl.Substring(opl.IndexOf(@"\") + 1);

但是在从 Windows 服务器发布和访问网站之后。我的用户名现在是“SRVCMAN”。

最佳答案

为此,请进入 IIS,点击 Authentication 并禁用 Anonymous Authentication 并启用 Windows Authentication

然后使用这段代码:

var ident = (System.Security.Principal.WindowsIdentity)HttpContext.Current.User.Identity;
If(!ident.IsAnonymous && ident.IsAuthenticated)
{
  var loginUsername = ident.Name;
}

关于c# - 如何在 ASP.net 中获取客户机用户登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6122915/

相关文章:

c# - 表中连续插入数据

c# - boolean 值的 GetHashCode 是如何实现的?

c# - 为什么 AutoFac 的 AsImplementedInterfaces 在另一种类型上会破坏我的解析器?

jquery - 如何将 JQuery 图像 slider 与母版页一起使用?

c# - 在 .net 代码中在运行时编辑 HTML 电子邮件模板

c# - 如何以编程方式修复非规范 ACL?

asp.net - 如何在 .asp 页面打开时调用 global.asax

c# - 在 aspnet core 运行时检查托管服务器是 IIS 还是 Kestrel

c# - 在具有 Windows 身份验证的 ASP.Net 5 中使用 IIS Express/Kestrel 时,User.IsInRole 始终为 false

c# - 如何使用 C# 在 IIS 中获取网站的 "Browse"URL?