如何获取访问ASP.NET应用程序的当前用户?

时间:2011-03-24 09:33:41

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

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

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

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

8 个答案:

答案 0 :(得分:48)

快速回答是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

答案 1 :(得分:18)

使用System.Web.HttpContext.Current.User.Identity.Name应该有效。 请执行以下操作,检查托管您站点的服务器上的IIS站点设置:

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

    IIS Settings

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

    Authentication

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

    domain\username

答案 2 :(得分:15)

如果您使用会员资格,可以执行:Membership.GetUser()

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

答案 3 :(得分:7)

最佳做法是首先检查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;
    }
}

答案 4 :(得分:5)

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

使用此:

HttpContext.Current.User.Identity.Name

答案 5 :(得分:2)

不要看得太远。

如果使用ASP.NET MVC进行开发,则只需将用户设置为property of the Controller class即可。因此,如果您在某些寻找当前用户的模型中迷路,请尝试后退并在控制器中获取相关信息。

在控制器中,只需使用:

using Microsoft.AspNet.Identity;

  ...

  var userId = User.Identity.GetUserId();
  ...

userId作为字符串。

答案 6 :(得分:1)

上面的一般共识答案似乎与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;

答案 7 :(得分:0)

我遇到了同样的问题。

这对我有用:

Setting up Authentication in IIS Panel

Setting up Properties of Windows Authentication in IIS

在IIS中设置Windows身份验证的属性 NTLM has to be the topmost

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