9

该站点在我的本地 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\网络服务

我怎样才能获得实际的 Windows 用户名,例如ourdomain/username

4

5 回答 5

15

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

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

于 2011-03-08T09:26:06.373 回答
4

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

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

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

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

另外,请查看MSDN中描述基本用户操作的此链接,

于 2011-03-08T09:28:48.040 回答
2

此片段显示如何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 身份验证 + 模拟,所以我会选择最后一个( WindowsIdentity.GetCurrent()),我确信它是正在运行的身份请求。

于 2011-03-08T09:25:22.473 回答
0

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

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

于 2011-03-08T09:39:40.297 回答
-3

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

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

于 2011-03-08T09:26:27.933 回答