在 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/