【问题标题】:Request.ServerVariables["LOGON_USER"] vs. Request.LogonUserIdentityRequest.ServerVariables["LOGON_USER"] 与 Request.LogonUserIdentity
【发布时间】:2015-07-01 22:35:00
【问题描述】:

我试图从 ASP.Net 应用程序的调用者那里获取当前的 WindowsIdentity,而无需模拟。

阅读一些文章后,我的设置是:

  • 在我的 IIS 中,我在身份验证设置中启用了 Windows 身份验证
  • 在我的 web.conf 中,我将身份验证模式设置为“Windows”

出于测试目的,我写了以下日志语句

m_techLogger.Warn(string.Format("Request[LOGON_USER] {0}", Request["LOGON_USER"]));
m_techLogger.Warn(string.Format("Request.LogonUserIdentity {0}", Request.LogonUserIdentity.Name));
m_techLogger.Warn(string.Format("HttpContext.Current.User.Identity {0}", HttpContext.Current.User.Identity.Name));
m_techLogger.Warn(string.Format("WindowsIdentity.GetCurrent() {0}", WindowsIdentity.GetCurrent().Name));

此语句返回以下内容

2015-04-23 10:47:19,628 [7] WARN  - Request[LOGON_USER] DOMAIN\User
2015-04-23 10:47:19,681 [7] WARN  - Request.LogonUserIdentity NT AUTHORITY\SYSTEM
2015-04-23 10:47:19,681 [7] WARN  - HttpContext.Current.User.Identity NT AUTHORITY\SYSTEM
2015-04-23 10:47:19,681 [7] WARN  - WindowsIdentity.GetCurrent() NT AUTHORITY\SYSTEM

我了解 WindowsIdentity.GetCurrent().Name 返回系统用户。我不明白为什么 Request.LogonUserIdentity 和 Request[LOGON_USER] 的输出不同。我需要来自用户的 WindowsIdentity 对象,其名称由 Request[LOGON_USER] 返回。

谁能指出我正确的方向?

【问题讨论】:

标签: c# asp.net .net authentication iis


【解决方案1】:

我有同样的问题。后来发现IIS中没有开启windows Authentication.

【讨论】:

    【解决方案2】:

    你尝试过用户

    User.Identity.Name 
    

    假设您是 Windows 用户,正如您所提到的。它给出了什么输出?

    另外,您的配置文件是否有这些设置:

    <authentication mode="Windows"/>
    <identity impersonate="true"/>
    

    【讨论】:

      【解决方案3】:

      System.Web.HttpContext.Current.User.Identity.Name

      获取或设置当前 HTTP 请求的安全信息。 (您网站上登录用户的名称)

      Request.ServerVariables

      获取 Web 服务器变量的集合。

      Request 属性提供对 HttpRequest 类的属性和方法的编程访问。由于 ASP.NET 页面包含对 System.Web 命名空间(其中包含 HttpContext 类)的默认引用,因此您可以在 .aspx 页面上引用 HttpRequest 的成员,而无需使用对 HttpContext 的完全限定类引用。

      结论 两者的工作原理相同,但是,Request.ServerVariables 您可以动态迭代整个集合。

      例如:

      int loop1, loop2;
      NameValueCollection coll;
      
      // Load ServerVariable collection into NameValueCollection object.
      coll=Request.ServerVariables; 
      // Get names of all keys into a string array. 
      String[] arr1 = coll.AllKeys; 
      for (loop1 = 0; loop1 < arr1.Length; loop1++) 
      {
         Response.Write("Key: " + arr1[loop1] + "<br>");
         String[] arr2=coll.GetValues(arr1[loop1]);
         for (loop2 = 0; loop2 < arr2.Length; loop2++) {
            Response.Write("Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>");
         }
      }
      

      【讨论】:

        【解决方案4】:

        Request["LOGON_USER"] 只是客户端发送给服务器的认证头。这意味着它是向您的服务器发送请求的客户端的登录名。除非您激活模拟,否则不会根据 Active Directory 验证此登录名。 更多信息在这里:https://msdn.microsoft.com/en-us/library/ms524602(v=vs.90).aspx

        现在如果不使用模拟,您就会陷入困境。您可以根据服务器上的 AD 检查 Request["LOGON_USER"] 中的用户。但我不建议你这样做。因为恶意客户端可以在该字段中发送任何用户名,并在该用户存在时登录您的服务器。

        这样做的正确方法是启用模拟,并且您使用 AD 组来允许用户执行您的服务现在正在执行的操作,然后只需将其添加到您的 IIS 配置中即可激活它

        <configuration>
          <system.web>
            <identity impersonate="true"/>
          </system.web>
        </configuration>
        

        但是,如果您真的不能使用模拟,您可以通过使用 Win32 API 模拟服务帐户来解决这个问题。 如果您想自己这样做,这里是 Microsoft https://msdn.microsoft.com/en-us/library/chf6fbt4.aspxhttps://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity.aspx 的示例

        或者你可以在这里找到一个好的包装器:How do you do Impersonation in .NET?

        使用它就这么简单:

        using (new Impersonation(domain, username, password))
        {
            // probably connecting to some bad 3rd party stuff that needs a very specific access.
        }
        

        现在在不了解您这样做的实际原因的情况下,我希望这将有助于您进一步前进,并且仅在绝对必要时才这样做

        【讨论】:

          【解决方案5】:

          当我尝试同样的方法时,我得到了

              Request.LogonUserIdentity.Name  "DOMAIN\\accountname"   (no capital letter)
              Request["LOGON_USER"]   "DOMAIN\\Accountname"   (capital letters)
          

          为了在我们的asp.net应用程序中获取当前用户,我使用了这行代码

          User.Identity.Name
          

          这有什么帮助吗?

          【讨论】:

            猜你喜欢
            • 2013-03-16
            • 1970-01-01
            • 1970-01-01
            • 2012-04-03
            • 1970-01-01
            • 1970-01-01
            • 2010-10-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多