How to get current user who's accessing an ASP.NET application?

前端 未结 8 1227
粉色の甜心
粉色の甜心 2020-11-29 06:24

To get the current logged in user at the system I use this code:

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


        
8条回答
  •  天涯浪人
    2020-11-29 07:12

    I ran in the same issue.

    This is what worked for me:

    Setting up Properties of Windows Authentication in IIS

    NTLM has to be the topmost. Further Web.config modifications, make sure you already have or add if these do not exist:

    
    
      
      
    
    
    
     
    
    
       
    
    

    See below a legit explanation for the two nodes and

    Difference between and ?

    And, of course , you get the username by

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

提交回复
热议问题