-
-
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; | |
} | |
} |
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.
Any Answers ?