C # asp.net getting username

"Have you ever done any .net programming? Yes? Well, here is a massive broken program, fix it." This is the situation I am in, so sorry if this is a simple question.

The program I'm working on pulls a file from a web server. It is expected that the user is already registered on the web server. I need to display the username of the current user registered on the server (or just make sure that someone is really registered on the server).

I tried the following and returns an empty string.

user = HttpContext.Current.User.Identity.Name; 
+6
source share
3 answers

Please make sure that you install Windows authentication in the Web.Config file. Also, before accessing the username, check the following:

 HttpContext.Current.User.Identity.IsAuthenticated 

Install Web.Config as follows:

 <authentication mode="Windows"></authentication> 
+7
source

First check the Web.config file for the <authentication> . If you did not find it, your application may not use any standard authentication mechanism. If so, take a look inside login.aspx or any other code that does authentication. There you will receive the data entered in the log.

I would not recommend that you change anything in the web.config file without having a clear understanding of what is happening in the application.

+1
source

The properties of the User object are usually populated with the authentication scheme of the application (Forms, Windows, or Custom), so you need to make sure that one of them is in place before you start accessing the User .

See the docs on MSDN for more information.

0
source

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


All Articles