【问题标题】:How to get at the current users windows identity?如何获取当前用户的windows身份?
【发布时间】:2011-03-08 09:14:47
【问题描述】:

该站点在我的本地 IIS 6.1 上运行。我想添加一些功能以从我们的 Active Directory (AD) 中提取信息。我的 AD 代码适用于许多其他项目和我的开发服务器。这是我写出用户名的尝试:

Response.Write("1. " + this.Request.LogonUserIdentity.Name);
Response.Write("2. " + Request.ServerVariables["Auth_User"]);
Response.Write("3. " + WindowsIdentity.GetCurrent().Name.ToString());

我得到的结果是:

  1. NT AUTHORITY\IUSR
  2. 管理员
  3. NT AUTHORITY\网络服务

我怎样才能获得像ourdomain/username这样的实际Windows用户名

【问题讨论】:

    标签: c# asp.net authentication kentico


    【解决方案1】:

    这里有两个不同的 windows 用户 - 第一个是您的应用程序用户,第二个是您的 ASP.NET 应用程序(从 IIS 角度来看的应用程序池)正在运行的用户(或 windows 帐户)。 WindowsIdentity.GetCurrent 通常会返回此引用。

    要获得使用该应用程序的实际 Windows 用户,您必须强制执行身份验证。为此,您可以在 IIS 中为所述网站启用集成身份验证(Windows 身份验证)。还要修改您的 ASP.NET 配置以使用 Windows 身份验证。现在您可以使用HttpContext.Current.User.Identity 获取实际用户。

    【讨论】:

      【解决方案2】:

      HttpContext.Current.User.Identity 在这里可能对你有用。

      System.Threading.Thread.CurrentPrincipal 同样可以提供帮助。

      但情况可能是您实际上必须在用户登录时设置一个身份实例(尽管不一定要实现 IPrincipal 和周边机制,而是使用内置的 WindowsIdentity 实现)。

      我对此不是 100%,Windows 身份验证可能会自动设置此功能,以便您轻松检索。

      另外,请查看描述基本用户操作的 this link from MSDN

      【讨论】:

        【解决方案3】:

        这个 sn-p 显示了如何设置 LogonUserIdentity(使用反射器)

         if ((this._wr is IIS7WorkerRequest) && (((this._context.NotificationContext.CurrentNotification == RequestNotification.AuthenticateRequest) && !this._context.NotificationContext.IsPostNotification) || (this._context.NotificationContext.CurrentNotification < RequestNotification.AuthenticateRequest)))
                {
                    throw new InvalidOperationException(SR.GetString("Invalid_before_authentication"));
                }
                IntPtr userToken = this._wr.GetUserToken();
                if (userToken != IntPtr.Zero)
                {
                    string serverVariable = this._wr.GetServerVariable("LOGON_USER");
                    string str2 = this._wr.GetServerVariable("AUTH_TYPE");
                    bool isAuthenticated = !string.IsNullOrEmpty(serverVariable) || (!string.IsNullOrEmpty(str2) && !StringUtil.EqualsIgnoreCase(str2, "basic"));
                    this._logonUserIdentity = CreateWindowsIdentityWithAssert(userToken, (str2 == null) ? "" : str2, WindowsAccountType.Normal, isAuthenticated);
                }
        

        如您所见,这已针对 IIS 7 进行了更改。 我相信您正在使用 Windows Authentication + Impersonation,所以我会选择 last one (WindowsIdentity.GetCurrent()),我确信这是正在运行的身份请求。

        【讨论】:

          【解决方案4】:

          首先,确保网站位于 Intranet 区域内(例如,只是 http://servername/ 而不是 http://www.servername.com/),或者您需要将 FQDN 添加到 IE 受信任的站点安全设置中。

          其次,如果您不使用 Intranet 区域,请确保 IE 正在发送凭据 - 工具 -> Internet 选项 -> 安全 -> 自定义级别 -> 用户身份验证 -> 登录 -> 使用当前用户名和密码自动登录

          【讨论】:

            【解决方案5】:

            用户是否使用他们的 AD 域/用户名登录到站点,如果是,那么此时获取用户名/域?

            如果不是,您将无法获取此信息,如果随机网站可以获取当前用户登录的域/用户名,那将是一个巨大的安全问题,不是吗?

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-11-05
              • 1970-01-01
              • 2018-06-03
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多