Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.net中获得客户端计算机用户登录?_C#_Asp.net_Iis_Local - Fatal编程技术网

C# 如何在ASP.net中获得客户端计算机用户登录?

C# 如何在ASP.net中获得客户端计算机用户登录?,c#,asp.net,iis,local,C#,Asp.net,Iis,Local,在本地主机上调用此代码时,我的用户名是“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)

在本地主机上调用此代码时,我的用户名是“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);
var ident = (System.Security.Principal.WindowsIdentity)HttpContext.Current.User.Identity;
If(!ident.IsAnonymous && ident.IsAuthenticated)
{
  var loginUsername = ident.Name;
}
或此代码:

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);
var ident = (System.Security.Principal.WindowsIdentity)HttpContext.Current.User.Identity;
If(!ident.IsAnonymous && ident.IsAuthenticated)
{
  var loginUsername = ident.Name;
}

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

我相信您正在寻求实现Windows身份验证模式。
请参阅以下文章并阅读相关内容:


要使其工作,请进入IIS,单击
身份验证
并禁用
匿名身份验证
并启用
Windows身份验证

然后使用以下代码:

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);
var ident = (System.Security.Principal.WindowsIdentity)HttpContext.Current.User.Identity;
If(!ident.IsAnonymous && ident.IsAuthenticated)
{
  var loginUsername = ident.Name;
}
我认为您试图获取这样的信息:


我只需要知道正在使用我的网站的用户。但是不同的人可以在不同的设备上使用相同的用户名。。在这里获取用户名有什么好处?只要我能获取他的用户名,他在哪台设备上都不重要。我在asp.net中开发了一个帮助台应用程序(用于企业的IT部门)。应用程序必须自动识别谁在使用它。这样用户就不必一次又一次地选择自己的名字。我想他不是在谈论Windows身份验证。请注意,要实现上述功能,您需要将您的网站配置为在IIS中使用集成安全性。@穆罕默德·阿赫塔:他希望在企业中验证用户身份,而不必让用户提供用户名和密码。我认为Windows身份验证在这种情况下是一个很好的选择。可能是,但从他的问题上看,没有任何东西表明他想要什么。但是我知道方法,并且在我的一个应用程序中嵌入了你回答的方法,我也有代码,但是我认为他只需要用户名,而不需要验证他的问题。这就是为什么我误解了这个问题。我在哪里可以找到认证?@ BababOM打开IIS并点击左边的站点,点击IIS类别下面的中间身份验证。