Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取当前用户的windows身份?_C#_Asp.net_Authentication_Kentico - Fatal编程技术网

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

C# 如何获取当前用户的windows身份?,c#,asp.net,authentication,kentico,C#,Asp.net,Authentication,Kentico,该站点正在我的本地IIS 6.1上运行。我想添加一些功能来从我们的Active Directory(AD)中提取信息。我的广告代码适用于许多其他项目和我的开发服务器。以下是我尝试写出用户名的步骤: Response.Write("1. " + this.Request.LogonUserIdentity.Name); Response.Write("2. " + Request.ServerVariables["Auth_User"])

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

Response.Write("1. " + this.Request.LogonUserIdentity.Name);
Response.Write("2. " + Request.ServerVariables["Auth_User"]);
Response.Write("3. " + WindowsIdentity.GetCurrent().Name.ToString());
我得到的结果是:

  • NT授权\IUSR
  • 管理员
  • NT授权\网络服务

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

    此代码片段显示了
    LogonUserIdentity
    是如何设置的(使用reflector)

    if((this._wr是IIS7WorkerRequest)和((this._context.NotificationContext.CurrentNotification==RequestNotification.AuthenticateRequest)和&!this._context.NotificationContext.IsPostNotification);(this._context.NotificationContext.CurrentNotification
    如您所见,IIS 7的这一点已更改。
    我相信您使用的是Windows身份验证+模拟,因此我将使用最后一个(
    WindowsIdentity.GetCurrent()
    ),我确信该身份验证请求正在运行

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


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

    用户是否使用其广告域/用户名登录到站点,如果是,请在此时获取用户名/域


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

    HttpContext.current.User.Identity
    可能对您有用

    同样地,
    System.Threading.Thread.CurrentPrincipal
    也会有所帮助

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

    我并不是百分之百支持这一点,Windows身份验证可能会自动设置这一点,以便您简单地检索


    此外,请查看描述基本用户操作的内容,

    首先,确保网站位于intranet区域内(例如,仅而不是),或者您需要将FQDN添加到IEs受信任的站点安全设置中

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

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