【问题标题】:How to get current user who's accessing an ASP.NET application?如何获取正在访问 ASP.NET 应用程序的当前用户?
【发布时间】:2011-07-21 23:42:25
【问题描述】:

要在系统中获取当前登录用户,我使用以下代码:

string opl = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

我在需要此信息的 ASP.NET 应用程序上工作。所以我把我的应用程序放在服务器上并尝试了上面的代码,我在字符串 opl 中得到了“网络服务”。我需要知道访问我的 ASP.NET 应用程序的 PC 的当前用户。

【问题讨论】:

标签: c# asp.net windows-server-2008


【解决方案1】:

快速回答是User = System.Web.HttpContext.Current.User

确保您的 web.config 具有以下身份验证元素。

<configuration>
    <system.web>
        <authentication mode="Windows" />
        <authorization>
            <deny users="?"/>
        </authorization>
    </system.web>
</configuration>

延伸阅读Recipe: Enabling Windows Authentication within an Intranet ASP.NET Web application

【讨论】:

  • 如果您正在寻找用户登录名。具体来说,字符串是System.Web.HttpContext.Current.User.Identity.Name
  • 现在它什么都不返回,字符串为空:s
  • 阅读文章,需要配置windows认证。
  • 我看过文章,默认设置windows认证
  • 您的 IIS 配置是否正确?您是否处于凭据可以“流”到服务器的 Intranet 场景中。 Windows 身份验证无法在更广泛的 Internet 上运行
【解决方案2】:

使用System.Web.HttpContext.Current.User.Identity.Name 应该可以。 请通过以下操作检查托管您网站的服务器上的 IIS 网站设置:

  1. 转到 IIS → 站点 → 您的站点 → 身份验证

  2. 现在检查 匿名访问 是否已禁用并且 Windows 身份验证 是否已启用。

  3. 现在System.Web.HttpContext.Current.User.Identity.Name 应该返回如下内容:

    <i>domain</i>\<i>username</i>

【讨论】:

    【解决方案3】:

    如果您使用会员资格,您可以这样做:Membership.GetUser()

    您的代码正在返回分配给 ASP.NET 的 Windows 帐户。

    附加信息编辑: 您将需要包括 System.Web.Security

    using System.Web.Security
    

    【讨论】:

    • 在哪个命名空间可以找到这个?
    • System.Web.Security 命名空间
    • 我得到:“对象引用未设置为对象的实例。”-错误
    • 您使用 ASP .NET 的会员功能吗?您使用哪种身份验证模式?
    • Windows 身份验证,我如何决定使用哪个功能?
    【解决方案4】:

    最佳做法是先检查Identity.IsAuthenticated 属性,然后像这样获取usr.UserName

    string userName = string.Empty;
    
    if (System.Web.HttpContext.Current != null && 
        System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
    {
        System.Web.Security.MembershipUser usr = Membership.GetUser();
        if (usr != null)
        {  
            userName = usr.UserName;
        }
    }
    

    【讨论】:

      【解决方案5】:

      您可以简单地使用页面的属性。有趣的是,您可以在代码中的任何位置访问该属性。

      使用这个:

      HttpContext.Current.User.Identity.Name
      

      【讨论】:

        【解决方案6】:

        别看得太远。

        如果您使用 ASP.NET MVC 进行开发,您只需将用户设为 property of the Controller class。所以万一你迷失在一些寻找当前用户的模型中,试着退后一步,在控制器中获取相关信息。

        在控制器中,只需使用:

        using Microsoft.AspNet.Identity;
        
          ...
        
          var userId = User.Identity.GetUserId();
          ...
        

        userId 作为字符串。

        【讨论】:

          【解决方案7】:

          上面的普遍共识答案似乎与 CORS 支持存在兼容性问题。为了使用 HttpContext.Current.User.Identity.Name 属性,您必须禁用匿名身份验证以强制 Windows 身份验证提供经过身份验证的用户信息。不幸的是,我认为您必须启用匿名身份验证才能在 CORS 场景中处理飞行前 OPTIONS 请求。

          您可以通过启用匿名身份验证并改用 HttpContext.Current.Request.LogonUserIdentity 属性来解决此问题。即使启用了匿名身份验证,这也会返回经过身份验证的用户信息(假设您处于 Intranet 场景中)。该属性返回一个 WindowsUser 数据结构,两者都在 System.Web 命名空间中定义

                  using System.Web;
                  WindowsIdentity user;
                  user  = HttpContext.Current.Request.LogonUserIdentity;
          

          【讨论】:

            【解决方案8】:

            我遇到了同样的问题。

            这对我有用:

            在 IIS 中设置 Windows 身份验证的属性

            NTLM 必须是最顶层的。 进一步的 Web.config 修改,如果这些不存在,请确保您已经拥有或添加:

            <system.web>
            
              <authentication mode="Windows" />
              <identity impersonate="true"/>
            
            </system.web>
            
             <!-- you need the following lines of code to bypass errors, concerning type of Application Pool (integrated pipeline or classic) -->
            
            <system.webServer>
               <validation validateIntegratedModeConfiguration="false"/>
            </system.webServer>
            

            请参阅下面对两个节点的合法解释和

            Difference between <system.web> and <system.webServer>?

            当然,您可以通过

            获得用户名
            //I am using the following to get the index of the separator "\\" and remove the Domain name from the string
            int indexOfSlashChar = HttpContext.Current.User.Identity.Name.IndexOf("\\"); 
            
            loggedInWindowsUserName = HttpContext.Current.User.Identity.Name.Substring(indexOfSlashChar + 1);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-12-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多