How to Get the Current Username in .Net Using C#

How do I get the current username in .NET using C#?

Option A)

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
  • Returns: NetworkName\Username
  • Gets the user's Windows logon name.
  • Details: https://docs.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity

Option B)

string userName = Environment.UserName
  • Returns: Username
  • Gets the user name of the person who is associated with the current thread.
  • Details: https://docs.microsoft.com/en-us/dotnet/api/system.environment.username

How to get current user in asp.net core


User.FindFirst(ClaimTypes.NameIdentifier).Value

EDIT for constructor

Below code works:

public Controller(IHttpContextAccessor httpContextAccessor)
{
var userId = httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value
}

Edit for RTM

You should register IHttpContextAccessor:

    public void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();
}

how to get current windows login user full name instead of domain name

You can get the users full name from the active directory (using System.DirectoryServices.AccountManagement):

PrincipalContext context = new PrincipalContext(ContextType.Domain,Environment.UserDomainName);
string loginName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
UserPrincipal user = UserPrincipal.FindByIdentity(context, loginName);
string userName = user.Name; //=user.GivenName + " " + user.Surname;

Getting Current User in Controller

You can get the current user id in your controller using the UserManager such as:

ASP.NET CORE >= 2.0

var currentUserId = User.FindFirstValue(ClaimTypes.NameIdentifier);

or Name using:

var currentUserId = User.FindFirstValue(ClaimTypes.Name);

If you want to get the current user in some other class, you can use the IHttpContextAccessor, passing it into the class's constructor and using the HttpContext to access the User

How to get current user who's accessing an ASP.NET application?

If you're using membership you can do: Membership.GetUser()

Your code is returning the Windows account which is assigned with ASP.NET.

Additional Info Edit:
You will want to include System.Web.Security

using System.Web.Security

How to get the current user in ASP.NET MVC

If you need to get the user from within the controller, use the User property of Controller. If you need it from the view, I would populate what you specifically need in the ViewData, or you could just call User as I think it's a property of ViewPage.

How to get the current logged in user ID in ASP.NET Core?

I included using System.Security.Claims and I could access the GetUserId() extension method

NB: I had the using Microsoft.AspNet.Identity already but couldn't get the extension method. So I guess both of them have to be used in conjunction with one another

using Microsoft.AspNet.Identity;
using System.Security.Claims;

EDIT:
This answer is now outdated. Look at Soren's or Adrien's answer for a dated way of achieving this in CORE 1.0



Related Topics



Leave a reply



Submit