How to implement User.Identity.Name in Global.asax in a new web application

In Visual Studio, I created a completely new ASP.NET Web Forms application using the template it provided.

I did nothing for the code that VS generates automatically, except for adding to Global.asax.cs

string myself = System.Security.Principal.WindowsIdentity.GetCurrent().Name; string me = User.Identity.Name; 

The string automatically saves my current Windows login ID. But string me is null, because when I debug, the user is null. However, Visual Studio does not detect errors before debugging.

How do I get the correct value for string me using User.Identity.Name?

+4
source share
2 answers

Entry code in Session_Start :

 void Session_Start(object sender, EventArgs e) { string myself = System.Security.Principal.WindowsIdentity.GetCurrent().Name; string me = User.Identity.Name; Response.Write("myself:" + myself + "<br>me:" + me); } 
+8
source

What authentication do you use? I don't remember off-hand, but anonymous authentication can represent a null or null object in User .

0
source

Source: https://habr.com/ru/post/1497513/


All Articles