ASP.NET Getting Current User Name

后端 未结 8 651
猫巷女王i
猫巷女王i 2021-01-12 10:57

I am trying to build an application on our company\'s intranet using ASP.NET and VB.NET.

Neither of these functions return anything once my application is published

8条回答
  •  感动是毒
    2021-01-12 11:46

    Disable Anonymous Authentication in IIS. User.Identity.Name might be empty if Anonymous Authentication is enabled in IIS.

    Set in web.config

    
      
        
             
                 
               
         
    
    

    Use User.Identity.Name to get the logon user.

    Environment.UserName is the running thread identity. If you have enabled Impersonation as Mark said, you can find out the returning result will be different. However this requires ASP.NET Impersionation. If you don't need ASP.NET Impersonation and dealing with the thread identity, you can ignore Environment.UserName if and just use User.Identity.Name.

    Also check before perform any action.

     if (User.Identity.IsAuthenticated)
        {
            Page.Title = "Home page for " + User.Identity.Name;
        }
        else
        {
            Page.Title = "Home page for guest user.";
        }
    

    Here is a good example

提交回复
热议问题