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
Reading CSV File and Storing Values into an Array
How to Remove Duplicates from a C# Array
Setting Authorization Header of Httpclient
How Expensive Are Exceptions in C#
How to Exclude Property from Json Serialization
Generating Random, Unique Values C#
What Is the Purpose of the "Prefer 32-Bit" Setting in Visual Studio and How Does It Actually Work
Is a Linq Statement Faster Than a 'Foreach' Loop
How Do Valuetypes Derive from Object (Referencetype) and Still Be Valuetypes
Assign Bitmapimage from Resources.Resx to Image.Source
How to Get the Cpu Usage in C#
Filtering Datagridview Without Changing Datasource
Difference Between Covariance & Contra-Variance
Ca2202, How to Solve This Case
Convert Datatable to Json in C#
Getting Servicestack to Retain Type Information