aspnetboilerplate - ASP NET Boilerplate, Login saved in ABPSession -
i'm new on asp net boilerplate framework, , created new mvc project multipage web application, without module zero.
i use abpsession class understand has inside user id taken on thread.currentprincipal.
however, not understand how after login, save user id in thread.currentprincipal.
i've searched in network , found several solutions, in abpsession class user id null.
the optimal solution found this:
ilist<claim> claimcollection = new list<claim> { new claim(abpclaimtypes.userid, "5") }; claimsidentity claimsidentity = new claimsidentity(claimcollection); var principal = new claimsprincipal(claimsidentity); thread.currentprincipal = principal; it's first time use principal , identity , despite being documented did not quite understand how use them asp net boilerplate, , did not find sample codes.
do know how tell me right way or tell me find functional codes?
thanks
altough question general, share code how add custom field abpsession in asp.net core.
myappsession.cs
//define own session , add custom field //then, can inject myappsession , use it's new property in project. public class myappsession : claimsabpsession, itransientdependency { public myappsession( iprincipalaccessor principalaccessor, imultitenancyconfig multitenancy, itenantresolver tenantresolver, iambientscopeprovider<sessionoverride> sessionoverridescopeprovider) : base(principalaccessor, multitenancy, tenantresolver, sessionoverridescopeprovider) { } public string useremail { { var useremailclaim = principalaccessor.principal?.claims.firstordefault(c => c.type == "application_useremail"); if (string.isnullorempty(useremailclaim?.value)) { return null; } return useremailclaim.value; } } } userclaimsprincipalfactory.cs
//override createasync method add custom claim public override async task<claimsprincipal> createasync(user user) { var claim = await base.createasync(user); claim.identities.first().addclaim(new claim("application_useremail", user.emailaddress)); return claim; }
Comments
Post a Comment