【问题标题】:Get windows username without using activex object不使用activex对象获取windows用户名
【发布时间】:2014-10-14 12:12:30
【问题描述】:

是否可以在不使用 activex 对象的情况下在远程服务器中托管的 asp.net 中获取客户端的 windows 用户名?

我使用了以下代码

        Response.Write("HttpContext.Current.Request.LogonUserIdentity.Name  " & HttpContext.Current.Request.LogonUserIdentity.Name)
        Response.Write("<br/>")
        Response.Write("HttpContext.Current.Request.IsAuthenticated " & HttpContext.Current.Request.IsAuthenticated)
        Response.Write("<br/>")
        Response.Write("HttpContext.Current.User.Identity.Name " & HttpContext.Current.User.Identity.Name)
        Response.Write("<br/>")
        Response.Write("System.Environment.UserName " & System.Environment.UserName)
        Response.Write("<br/>")
        Response.Write("Security.Principal.WindowsIdentity.GetCurrent().Name() " & System.Security.Principal.WindowsIdentity.GetCurrent().Name())
        Response.Write("<br/>")

但它总是给我服务器用户名而不是客户端用户名。

【问题讨论】:

  • 您的虚拟目录/应用程序是如何配置的?
  • 只有在使用 Windows 身份验证时才有可能。
  • 这是可能的,但您访问此信息的方式在每个浏览器中都不同(如果它甚至存在的话)。例如 Mozilla FireFox 你使用nsIEnvironment。因此,您的 javascript 将首先检测浏览器,然后相应地访问 windows 用户名。此外,如果您使用 C#,您可以使用 WAFFLE Source
  • @AdrianoRepetti 使用 Windows 身份验证仅适用于 Intranet,但不适用于 Internet 不是吗?
  • 是的,仅在 Intranet 中,因为无法从不受信任的站点使用 JavaScript 访问该信息可能是一个安全问题。

标签: javascript asp.net windows iis activexobject


【解决方案1】:

你可以尝试使用ServerVariables

string Hostname=Request.ServerVariables["REMOTE_HOST"];
string Username=Request.ServerVariables["REMOTE_USER"];

【讨论】:

  • "[REMOTE_USER] 在用户名映射到 Windows 帐户之前,用户名源自客户端发送的授权标头 . 如果您的 Web 服务器上安装了将传入用户映射到帐户的身份验证过滤器,请使用 LOGON_USER 查看映射的用户名。"
【解决方案2】:

如果您使用“Windows 身份验证”,此行将起作用

Response.Write("HttpContext.Current.User.Identity.Name " & HttpContext.Current.User.Identity.Name)

参考:MSDN

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2015-03-05
    相关资源
    最近更新 更多