ASP.NET Getting Current User Name

后端 未结 8 653
猫巷女王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:39

    I tried all of the above and none of them worked. I also could not get into my IIS to change the settings. I struggled and struggled and struggled with this. I also searched a long time without finding the answer. One of the things is that I don't have access to IIS, that is locked down, so I couldn't change any of the server settings. I had to go with what I was capable of doing in code. When I researched it, many of the replies said, "set up IIS like this". . .well, that's great when you have access to IIS, but I didn't -- I had to work with what I could do in code. So, I ended up handling it like this:

    In my web config file, I added the following lines of code within the section:

    
     
      
       
       
      
    
    
    

    Then, it returned an error on my local, which I had to go in and fix. I went to the applicationhost.config file located in the following path on my machine (yours might be different):

    C:\users\"your user name"\My Documents\"yourIISInstallation"\config\applicationhost.config

    and I changed the following settings to "allow", which had been set to "deny":

    changed to

    and

    changed to:

    in the

    
    

    section. Before I found out this fix, I was pulling my hair out over this. I hope this helps someone. As soon as I put in the above code into the webconfig file, it worked on the intranet, it just returned errors in my local, but as soon as I added the above to my local applicationhost.config file, it started working on my local as well. Then, I called the following variable to return the name of the logged in user on windows:

    HttpContext.Current.User.Identity.Name.ToString().Substring((HttpContext.Current.User.Identity.Name.ToString().IndexOf("\\")) + 1);
    

    Cheers!

提交回复
热议问题