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

时间:2011-05-25 10:22:53

标签: 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 Server发布和访问网站之后。我的用户名现在是'SRVCMAN'。

3 个答案:

答案 0 :(得分:4)

要实现此目的,请访问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;
}

答案 1 :(得分:4)

    // will return the host name making the request

    string s = Request.ServerVariables["REMOTE_HOST"] ;
-----------------------------------------------------------------
    // will return the computer name

    string s = Request.ServerVariables["SERVER_NAME"] ;
-----------------------------------------------------------------
   //will return Windows account for the user.

    string s = Request.ServerVariables["AUTH_USER"] ;
-----------------------------------------------------------------

我认为您尝试获取此类信息:

IIS Server Variables

答案 2 :(得分:3)