Created
January 13, 2016 16:25
-
-
Save hikalkan/67469e05475c2d18cb88 to your computer and use it in GitHub Desktop.
Adding a new property to session
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
//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 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 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; | |
} | |
} |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to update Thread.CurrentPrincipal when the user information changes.(The user logs in and updates his own email or active Organization)