如何获得在当前用户的Windows标识 [英] How to get at the current users windows identity

查看:377
本文介绍了如何获得在当前用户的Windows标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该网站是在我的本地IIS 6.1上运行。我想添加一些功能,以从我们的广告中提取信息。我的广告code工作在许多其他项目和我的开发服务器上。下面是我在尝试写出的用户名:

The site is running on my local IIS 6.1. I Would like to add some features to pull information from our AD. My AD code works on many other projects and on my dev 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. 管理员

  3. NT AUTHORITY \\ NETWORK SERVICE

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

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

感谢

推荐答案

这个片断显示了如何 LogonUserIdentity 终止(使用反射)设置

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

正如你可以看到这个已更改IIS 7。
我相信你正在使用的 Windows身份验证+模拟,所以我会去的最后一个 WindowsIdentity.GetCurrent() ),我敢肯定是与正在运行的身份请求。

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.

这篇关于如何获得在当前用户的Windows标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆