docs/en/framework/infrastructure/current-user.md
//[doc-seo]
{
"Description": "Learn how to access information about the logged-in user in your ABP Framework app using the ICurrentUser service."
}
It is very common to retrieve the information about the logged in user in a web application. The current user is the active user related to the current request in a web application.
ICurrentUser is the main service to get info about the current active user.
Example: Injecting the ICurrentUser into a service:
using System;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Users;
namespace AbpDemo
{
public class MyService : ITransientDependency
{
private readonly ICurrentUser _currentUser;
public MyService(ICurrentUser currentUser)
{
_currentUser = currentUser;
}
public void Foo()
{
Guid? userId = _currentUser.Id;
}
}
}
Common base classes have already injected this service as a base property. For example, you can directly use the CurrentUser property in an application service:
using System;
using Volo.Abp.Application.Services;
namespace AbpDemo
{
public class MyAppService : ApplicationService
{
public void Foo()
{
Guid? userId = CurrentUser.Id;
}
}
}
Here are the fundamental properties of the ICurrentUser interface:
true if the current user has logged in (authenticated). If the user has not logged in then Id and UserName returns null.null, if the current user has not logged in.null, if the current user has not logged in.null, if the current user is not assigned to a tenant.null, if the current user has not logged in or not set an email address.true, if the email address of the current user has been verified.null, if the current user has not logged in or not set a phone number.true, if the phone number of the current user has been verified.ICurrentUser is implemented on the ICurrentPrincipalAccessor (see the section below) and works with the claims. So, all of the above properties are actually retrieved from the claims of the current authenticated user.
ICurrentUser has some methods to directly work with the claims, if you have custom claims or get other non-common claim types.
null if not found.Beside these standard methods, there are some extension methods:
null if not found. It has a generic overload that also casts the value to a specific type.Id of the current user. If the current user has not logged in, it throws an exception (instead of returning null) . Use this only if you are sure that the user has already authenticated in your code context.ICurrentUser works independently of how the user is authenticated or authorized. It seamlessly works with any authentication system that works with the current principal (see the section below).
ICurrentPrincipalAccessor is the service that should be used (by the ABP and your application code) whenever the current principal of the current user is needed.
For a web application, it gets the User property of the current HttpContext. For a non-web application, it returns the Thread.CurrentPrincipal.
You generally don't need to use this low level
ICurrentPrincipalAccessorservice and just directly work with theICurrentUserexplained above.
You can inject ICurrentPrincipalAccessor and use the Principal property to get the current principal:
public class MyService : ITransientDependency
{
private readonly ICurrentPrincipalAccessor _currentPrincipalAccessor;
public MyService(ICurrentPrincipalAccessor currentPrincipalAccessor)
{
_currentPrincipalAccessor = currentPrincipalAccessor;
}
public void Foo()
{
var allClaims = _currentPrincipalAccessor.Principal.Claims.ToList();
//...
}
}
Current principal is not something you want to set or change, except at some advanced scenarios. If you need it, use the Change method of the ICurrentPrincipalAccessor. It takes a ClaimsPrincipal object and makes it "current" for a scope.
Example:
public class MyAppService : ApplicationService
{
private readonly ICurrentPrincipalAccessor _currentPrincipalAccessor;
public MyAppService(ICurrentPrincipalAccessor currentPrincipalAccessor)
{
_currentPrincipalAccessor = currentPrincipalAccessor;
}
public void Foo()
{
var newPrincipal = new ClaimsPrincipal(
new ClaimsIdentity(
new Claim[]
{
new Claim(AbpClaimTypes.UserId, Guid.NewGuid().ToString()),
new Claim(AbpClaimTypes.UserName, "john"),
new Claim("Claim", "42")
}
)
);
using (_currentPrincipalAccessor.Change(newPrincipal))
{
var userName = CurrentUser.UserName; //returns "john"
//...
}
}
}
Use the Change method always in a using statement, so it will be restored to the original value after the using scope ends.
This can be a way to simulate a user login for a scope of the application code, however try to use it carefully.
AbpClaimTypes is a static class that defines the names of the standard claims and used by the ABP.
UserName, UserId, Role and Email properties are set from the System.Security.Claims.ClaimTypes class, but you can change them.EmailVerified, PhoneNumber, TenantId... are defined by the ABP by following the standard names wherever possible.It is suggested to use properties of this class instead of magic strings for claim names.