Linked Questions

Popular Questions

ServiceStack caching users roles and permissions approach

Asked by At

With the AuthFeature / AuthUserSession plugin, we can populate the session with a users roles, permissions, etc in the PopulateSessionFilter on each request.

Plugins.Add(new AuthFeature(() => new AuthUserSession(), 
                new IAuthProvider[] {
                    new CredentialsAuthProvider(AppSettings), 
                    new NetCoreIdentityAuthProvider(AppSettings) 
                    {
                        PopulateSessionFilter = (session, principal, req) => 
                        {
                            //Example of populating ServiceStack Session Roles for EF Identity DB
                            var userManager = req.TryResolve<UserManager<ApplicationUser>>();
                            var user = userManager.FindByIdAsync(session.Id).Result;
                            var roles = userManager.GetRolesAsync(user).Result;
                            session.Roles = roles.ToList();
                        }
                    }, 
                }));

Is there way to store this in cache, MemoryCacheClient or Redis depending on what has been configured so do not have to do a database call here, or is it a case of implementing the caching solution in the userManager itself (or whatever repository code is written to get this information)?

Related Questions