How to get current Windows Login Name in C# Web APP?

后端 未结 3 1483
忘掉有多难
忘掉有多难 2020-12-20 20:38

How can I get the user name of Windows Login? I used these methods:

  • Environment.UserName
  • WindowsIdentity.GetCurrent().Name;

But, when I

3条回答
  •  独厮守ぢ
    2020-12-20 21:21

    Take a look at:

    https://richhewlett.com/2011/02/15/getting-a-users-username-in-asp-net/

    Here are the main examples from the link above:

    Scenario 1: Anonymous Authentication in IIS with impersonation off.

    HttpContext.Current.Request.LogonUserIdentity.Name  -> COMPUTER1\IUSR_COMPUTER1
    HttpContext.Current.Request.IsAuthenticated -> False
    HttpContext.Current.User.Identity.Name  -> –
    System.Environment.UserName -> ASPNET
    Security.Principal.WindowsIdentity.GetCurrent().Name    -> COMPUTER1\ASPNET
    

    Scenario 2: Windows Authentication in IIS, impersonation off.

    HttpContext.Current.Request.LogonUserIdentity.Name  -> MYDOMAIN\USER1
    HttpContext.Current.Request.IsAuthenticated -> True
    HttpContext.Current.User.Identity.Name  -> MYDOMAIN\USER1
    System.Environment.UserName -> ASPNET
    Security.Principal.WindowsIdentity.GetCurrent().Name    -> COMPUTER1\ASPNET
    

    Scenario 3: Anonymous Authentication in IIS, impersonation on

    HttpContext.Current.Request.LogonUserIdentity.Name  -> COMPUTER1\IUSR_COMPUTER1
    HttpContext.Current.Request.IsAuthenticated -> False
    HttpContext.Current.User.Identity.Name  -> –
    System.Environment.UserName -> IUSR_COMPUTER1
    Security.Principal.WindowsIdentity.GetCurrent().Name    -> COMPUTER1\IUSR_COMPUTER1
    

    Scenario 4: Windows Authentication in IIS, impersonation on

    HttpContext.Current.Request.LogonUserIdentity.Name  -> MYDOMAIN\USER1
    HttpContext.Current.Request.IsAuthenticated -> True
    HttpContext.Current.User.Identity.Name  -> MYDOMAIN\USER1
    System.Environment.UserName -> USER1
    Security.Principal.WindowsIdentity.GetCurrent().Name    -> MYDOMAIN\USER1
    

    Note:

    SERVER1\ASPNET: Identity of the running process on server.
    SERVER1\IUSR_SERVER1: Anonymous guest user defined in IIS.
    MYDOMAIN\USER1: The user of the remote client.
    

提交回复
热议问题