1

I'm having a requirement to identify the current logged in user in Ubuntu system. In windows I can use

System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

to get the username. I'm confused how to do that for Linux. Any suggestions?

I want the current logged in user in the controller.

public ControllerName(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
            webRootPath = _hostingEnvironment.WebRootPath; 
            var user = User.FindFirst(ClaimTypes.Name).Value;/* System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(); */
        }

But User is returning null always.

7
  • Can you share more details code of where do you want to get the current logged in user? Commented Jan 7, 2019 at 6:35
  • @TanvirArjel I've edited the question? Commented Jan 7, 2019 at 6:51
  • Are you using ASP.NET Core identity for login? Commented Jan 7, 2019 at 6:53
  • @TanvirArjel No. I'm not using it. In that case is there any other way to get the user name? Commented Jan 7, 2019 at 6:54
  • Oh! then how do you storing logged-in user information in session? What is your logged in mechanism? Commented Jan 7, 2019 at 6:55

2 Answers 2

1

If you just want the user name:

Environment.UserName
Sign up to request clarification or add additional context in comments.

Comments

0

I solved the problem by executing terminal command whoami from the .Net core application.

You can refer the below links for some idea.

1)run bash command from .NetCore with arguments

2)https://loune.net/2017/06/running-shell-bash-commands-in-net-core/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.