Last active
April 27, 2021 13:18
-
-
Save ismcagdas/6f2a9a3b5d7b907cb8d94a72124d59a1 to your computer and use it in GitHub Desktop.
How to add custom field to AbpSession in ASP.NET Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Define your own session and add your custom field to it | |
//Then, you can inject MyAppSession and use it's new property in your project. | |
public class MyAppSession : ClaimsAbpSession, ITransientDependency | |
{ | |
public MyAppSession( | |
IPrincipalAccessor principalAccessor, | |
IMultiTenancyConfig multiTenancy, | |
ITenantResolver tenantResolver, | |
IAmbientScopeProvider<SessionOverride> sessionOverrideScopeProvider) : | |
base(principalAccessor, multiTenancy, tenantResolver, sessionOverrideScopeProvider) | |
{ | |
} | |
public string UserEmail | |
{ | |
get | |
{ | |
var userEmailClaim = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == "Application_UserEmail"); | |
if (string.IsNullOrEmpty(userEmailClaim?.Value)) | |
{ | |
return null; | |
} | |
return userEmailClaim.Value; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Override CreateAsync method to add your custom claim | |
public override async Task<ClaimsPrincipal> CreateAsync(User user) | |
{ | |
var claim = await base.CreateAsync(user); | |
claim.Identities.First().AddClaim(new Claim("Application_UserEmail", user.EmailAddress)); | |
return claim; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ismcagdas May you give me an example, how can I use it in my own code.