Skip to content

Instantly share code, notes, and snippets.

@hikalkan
Created January 13, 2016 16:25
Show Gist options
  • Save hikalkan/67469e05475c2d18cb88 to your computer and use it in GitHub Desktop.
Save hikalkan/67469e05475c2d18cb88 to your computer and use it in GitHub Desktop.
Adding a new property to session
//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;
}
}
@hikalkan
Copy link
Author

  • I created a new template from http://www.aspnetboilerplate.com/Templates (included module zero)
  • Changed AccountController.SignInAsync method as shown above to add a new claim.
  • Created a new class (MyAppSession) to get claim value.
  • SessionAppService is a sample class injects MyAppSession and gets user's email.

@iyhammad
Copy link

Thank you for your wonderful work...

@muhlisatac
Copy link

Great job. Thank you so much...

@hikalkan
Copy link
Author

You're welcome :)

@maharatha
Copy link

You rock..

@jmhinnen
Copy link

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!

@bhaidar
Copy link

bhaidar commented Mar 13, 2017

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

@ismcagdas
Copy link

@bhaidar
Copy link

bhaidar commented Mar 14, 2017

Oh thanks :-)

Why that document not showing in any menu for the documentation? I am sure there are more hidden gems out there :)

@jmhinnen
Copy link

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;
}

@maliming
Copy link

maliming commented May 5, 2017

@hikalkan
how to update Thread.CurrentPrincipal when the user information changes.(The user logs in and updates his own email)

@bbakermmc
Copy link

This method doesn't exist in ASPNetZero 4.1.4 what's the proper way now?

@dineplan
Copy link

Any Answers ?

@karakule006
Copy link

how to update Thread.CurrentPrincipal when the user information changes.(The user logs in and updates his own email or active Organization)

@jacobmhulse
Copy link

Any luck figuring out how to update these values?

@wuball
Copy link

wuball commented Apr 13, 2018

@hikalkan @ismcagdas
How to automatically inject like "AbpSession", otherwise add "MySession" parameter in each AppService.

@maoyuan121
Copy link

@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
}

@pratiksa
Copy link

pratiksa commented May 23, 2018

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:

  1. 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 .
  2. After adding new claims then how will we get the values in UI using AbpSession.
  3. How AbpSession Works in Asp.Net.Zero tool.
  4. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment