【问题标题】:Retreving Client Username by using Windows authentication使用 Windows 身份验证检索客户端用户名
【发布时间】:2009-05-13 10:13:01
【问题描述】:

我正在尝试检索在 ASP.NET 中登录到我们 Intranet 上的计算机的人员的用户名和客户端计算机名。这仅用于记录目的。我检索了用户名“System.Security.Principal.WindowsIdentity.GetCurrent().Name”,问题是访问此站点的所有人都显示相同的用户名(即我部署应用程序的服务器名称)。请帮忙。我在 web.config 中使用 windows 身份验证模式。

【问题讨论】:

    标签: asp.net asp.net-membership


    【解决方案1】:

    用户名可以是@Mehrdad 所描述的。对于用户机器的名称,您可以像这样使用 HttpRequest 对象:

    if(Request.IsAuthenticated)
        string userName = Request.LogonUserIdentity.Name;
    string machineAddress = Request.UserHostAddress;
    string machineName = Request.UserHostName;
    

    (编辑)

    在 web.config 文件中,我使用了这一行:

    <system.web>
        <authentication mode="Windows"/>
    </system.web>
    

    在我使用的 default.aspx.cs 中:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
    
            if (Request.IsAuthenticated)
            {
                sb.AppendFormat("User Name: {0}<br/>", Request.LogonUserIdentity.Name);
            }
            else
            {
                sb.Append("Request not authenticated");
            }
    
            sb.AppendFormat("Machine Address: {0}<br/>", Request.UserHostAddress);
            sb.AppendFormat("Machine Name:    {0}<br/>", Request.UserHostName);
    
            lblTest.Text = sb.ToString();
        }
    }
    

    这是产生以下输出:

    用户名:HPAS\amantur

    机器地址:127.0.0.1

    机器名称:127.0.0.1

    【讨论】:

    • 我很努力,但如果 IIS 设置中缺少任何东西,它的效果会不一样。感谢您的帮助。
    • 感谢您的帮助。我尝试了此代码,但它说“请求未通过身份验证”是我遗漏任何东西。我收到此消息。请帮助
    • 知道了!!您需要禁用从 IIS 管理器对 Web 应用程序的匿名访问。因为如果启用了匿名访问并且您不需要输入用户 ID/密码并且 Request.IsAuthenticated 为 false,因为它正在使用匿名配置文件。
    • 感谢您的帮助,您能确认一件事,当系统在域下时,Windows 身份验证是否有效?
    • 当 PC 在域中以及在工作组(或个人)系统上时,它可以工作。在域中,您登录由域控制器验证,否则由您访问网站的系统验证。
    【解决方案2】:

    您使用的代码将获得与当前线程关联的WindowsIdentity(这是运行 ASP.NET 的标识)。除非您基于不起作用的客户端用户身份进行模拟。你需要使用这个:

    HttpContext.Current.User.Identity.Name
    

    【讨论】:

      猜你喜欢
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多