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