How to get user name using Windows authentication in asp.net?

前端 未结 7 1843
抹茶落季
抹茶落季 2020-12-23 11:43

I want to get user name using Windows authentication

Actually I implemented \"Sign in as different user\",when click this button Windows security will appear there w

相关标签:
7条回答
  • 2020-12-23 12:23

    This should work:

    User.Identity.Name
    

    Identity returns an IPrincipal

    Here is the link to the Microsoft documentation.

    0 讨论(0)
  • 2020-12-23 12:27

    It depends on the configuration of the application as well as on IIS as this gentleman in the below link has rightfully explained. Please see his article below

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

    0 讨论(0)
  • 2020-12-23 12:27

    You can read the Name from WindowsIdentity:

    var user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    return Ok(user);
    
    0 讨论(0)
  • 2020-12-23 12:28

    I think because of the below code you are not getting new credential

    string fullName = Request.ServerVariables["LOGON_USER"];
    

    You can try custom login page.

    0 讨论(0)
  • 2020-12-23 12:28

    Username you get like this:

    var userName = HttpContext.Current.Request.LogonUserIdentity?.Name;
    
    0 讨论(0)
  • 2020-12-23 12:30

    You can get the user's WindowsIdentity object under Windows Authentication by:

    WindowsIdentity identity = HttpContext.Current.Request.LogonUserIdentity;
    

    and then you can get the information about the user like identity.Name.

    Please note you need to have HttpContext for these code.

    0 讨论(0)
提交回复
热议问题