-
-
Save maliming/0075b3acee978632baaad151be46ae41 to your computer and use it in GitHub Desktop.
Adding a new property to session
This file contains hidden or 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
| //Add new property to claims on login | |
| private async Task SignInAsync(User user, ClaimsIdentity identity = null, bool rememberMe = false) | |
| { | |
| if (identity == null) | |
| { | |
| identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); | |
| } | |
| identity.AddClaim(new Claim("Application_UserEmail", user.EmailAddress)); //SETTING NEW PROPERTY | |
| AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); | |
| AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = rememberMe }, identity); | |
| } |
This file contains hidden or 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
| //Create a custom session class | |
| public class MyAppSession : ITransientDependency | |
| { | |
| public string UserEmail | |
| { | |
| get | |
| { | |
| var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal; | |
| if (claimsPrincipal == null) | |
| { | |
| return null; | |
| } | |
| var emailClaim = claimsPrincipal.Claims.FirstOrDefault(c => c.Type == "Application_UserEmail"); | |
| if (emailClaim == null || string.IsNullOrEmpty(emailClaim.Value)) | |
| { | |
| return null; | |
| } | |
| return emailClaim.Value; | |
| } | |
| } | |
| } |
This file contains hidden or 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
| //Getting session property using MyAppSession | |
| [AbpAuthorize] | |
| public class SessionAppService | |
| { | |
| private readonly MyAppSession _mySession; | |
| public SessionAppService(MyAppSession mySession) | |
| { | |
| _mySession = mySession; | |
| } | |
| public void Test() | |
| { | |
| var userEmailFromSession = _mySession.UserEmail; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment