abp icon indicating copy to clipboard operation
abp copied to clipboard

Maybe I want to know that how to set Current User

Open wingqing1314 opened this issue 6 years ago • 4 comments

See https://docs.abp.io/en/abp/latest/Entities#base-classes-interfaces-for-audit-properties

Originally posted by @liangshiw in https://github.com/abpframework/abp/issues/4286#issuecomment-641677741

Thanks liang, but the doc won't solve my problem.

wingqing1314 avatar Jun 10 '20 03:06 wingqing1314

Please read the document first : https://docs.abp.io/en/abp/latest/CurrentUser

realLiangshiwei avatar Jun 10 '20 03:06 realLiangshiwei

Please read the document first : https://docs.abp.io/en/abp/latest/CurrentUser

wow ,hot and fresh!

wingqing1314 avatar Jun 10 '20 03:06 wingqing1314

Please read the document first : https://docs.abp.io/en/abp/latest/CurrentUser

Sorry , I still don't understand that how/where to set Current User 's properties.

wingqing1314 avatar Jun 10 '20 04:06 wingqing1314

Solved!

Add the code in OnApplicationInitialization method of HostModule

app.Use(async (ctx, next) =>
            {
                var currentPrincipalAccessor = ctx.RequestServices.GetRequiredService<ICurrentPrincipalAccessor>();
                var map = new Dictionary<string, string>()
                {
                    { "userId", AbpClaimTypes.UserId },
                    { "role", AbpClaimTypes.Role },
                    { "userNo", AbpClaimTypes.UserName },
                    //any other map
                };
                var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => map.Keys.Contains(p.Type)).ToList();
                currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim(map[p.Type], p.Value, p.ValueType, p.Issuer))));
                await next();
            });

wingqing1314 avatar Jun 10 '20 07:06 wingqing1314