如何获取当前用户的windows身份?

发布于 2024-10-20 22:59:16 字数 494 浏览 2 评论 0原文

该网站在我的本地 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\NETWORK SERVICE

如何获取实际的 Windows 用户名,如 ourdomain/username

The site is running on my local IIS 6.1. I Would like to add some features to pull information from our Active Directory (AD). My AD code works on many other projects and on my development server. Here are my attempts at writing out the username:

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

The results I get are:

  1. NT AUTHORITY\IUSR
  2. administrator
  3. NT AUTHORITY\NETWORK SERVICE

How can I get at the actual windows username like ourdomain/username

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

看轻我的陪伴 2024-10-27 22:59:16

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

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

There are two different windows user here - first one is your application user and second is user (or windows account) under which your ASP.NET application (application pool from IIS perspective) is running. WindowsIdentity.GetCurrent will typically return this reference.

To getting actual windows user that using the application, you must enforce authentication. To do that, you can enable integrated authentication (windows authentication) in IIS for the said web site. Also modify your ASP.NET configuration to use windows authentication. Now you can use HttpContext.Current.User.Identity to get the actual user.

狂之美人 2024-10-27 22:59:16

HttpContext.Current.User.Identity 可能对您有用。

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

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

我并不是 100% 同意这一点,Windows 身份验证可能会自动设置此项以便您轻松检索。

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

HttpContext.Current.User.Identity may be of use to you here.

Likewise System.Threading.Thread.CurrentPrincipal could help.

But the case might be that you actually have to set an identity instance as the user logs in (though not necessarily implement IPrincipal and the surrounding mechanisms, rather using the built-in WindowsIdentity implementation).

I'm not 100% percent on this, Windows Authentication might set this automatically for you to simply retrieve.

Also, check out this link from MSDN which describes basic user operations,

泪是无色的血 2024-10-27 22:59:16

此代码段显示了如何设置 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()),我确信它是正在运行的身份请求。

This snippet shows how LogonUserIdentity is set (using reflector)

 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);
        }

As you can see this has been changed for IIS 7.
I believe you are using Windows Authentication + Impersonation so I would go with the last one (WindowsIdentity.GetCurrent()) which I am sure is the identity request being run with.

怪我鬧 2024-10-27 22:59:16

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

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

Firstly, make sure that the website is within the intranet zone (eg just http://servername/ rather than http://www.servername.com/), or you need to add your FQDN to IEs Trusted Sites security settings.

Secondly, if you are not using the intranet zone, make sure IE is sending the credentials - Tools -> Internet Options -> Security -> Custom Level -> User Authentication -> Logon -> Automatic Logon with Current Username And Password

剩一世无双 2024-10-27 22:59:16

用户是否使用其 AD 域/用户名登录网站,如果是,则获取此时的用户名/域?

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

Is the user logging in to the site using their AD domain/username, if so grab the username/domain at that point?

If not you won't be able to get this, if a random website could grab the domain/username of the current users login that would be a huge security issue wouldn't it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文