-
-
Save hikalkan/67469e05475c2d18cb88 to your computer and use it in GitHub Desktop.
//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); | |
} |
//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; | |
} | |
} | |
} |
//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; | |
} | |
} |
You rock..
This no longer works because the Login method in the controller no longer calls SignInAsync. It's calling the _signInManager.SignInOrTwoFactor. Where are we supposed to add in the custom claim with the SignInManager? I assume we need to override some method in the SignInManager but just making sure before I start to tear apart the code.
Thanks for the help!
Great job, thanks!
Is it possible to share with us a sample on extending User object with additional fields? Hence, a user might need to edit those additional fields also. Thanks
Hi,
You can check this document https://aspnetzero.com/Documents/Extending-Existing-Entities.
Oh thanks :-)
Why that document not showing in any menu for the documentation? I am sure there are more hidden gems out there :)
The original code will no longer work by adding your claim into the AccountController. The sign in code has moved to SignInManager. SignInManager now calls CreateIdentityAsync on the UserManager class. To add a custom claim, just override this method and add your claim. You can see that even the method inside AccountController calls CreateIdentityAsync so even when someone logs in through the Forgot password link, it will add the custom claim properly.
UserManager:
public override async Task<ClaimsIdentity> CreateIdentityAsync(User user, string authenticationType)
{
var identity = await base.CreateIdentityAsync(user, authenticationType);
//- Custom claim here
identity.AddClaim(new Claim("Application_UserEmail", user.EmailAddress)); //SETTING NEW PROPERTY
return identity;
}
@hikalkan
how to update Thread.CurrentPrincipal when the user information changes.(The user logs in and updates his own email)
This method doesn't exist in ASPNetZero 4.1.4 what's the proper way now?
Any Answers ?
how to update Thread.CurrentPrincipal when the user information changes.(The user logs in and updates his own email or active Organization)
Any luck figuring out how to update these values?
@hikalkan @ismcagdas
How to automatically inject like "AbpSession", otherwise add "MySession" parameter in each AppService.
@wuball custom your own AppService base class and inject you own xxSession to the base class, last make your all appservice inherit the base class
public class WBAppService : AbpServiceBase
{
public IYourSession YourSession { get; private set; }
public WBAppService (IYourSession yourSession)
{
YourSession = yourSession
}
}
public class YourAppService : WBAppService
{
// Now, you can use it
}
I want to add my own properties in "IAbpSession" interface and i have taken this url "https://gist.github.com/hikalkan/67469e05475c2d18cb88" for reference and below are my project details and i have few questions on this-
Project Details:
i) Created Project Structure using Asp.Net Zero tool
ii) Using Asp.Net Core
iii) UI Angualr-5
Questions:
- I have created all things as per given in above url. But i am not getting the definition for "CreateIdentityAsync" So where i need to exactly place this method .
- After adding new claims then how will we get the values in UI using AbpSession.
- How AbpSession Works in Asp.Net.Zero tool.
- How we will get the value using abpSession for newly added claim in angular-5 .
Can any one provide the code snippet using angular-5 and asp.net zero tool.
You're welcome :)