예제 #1
0
        public static async Task CookieSignin(CookieSigningInContext context)
        {
            UserManager <Models.ApplicationUser>   userManager   = context.HttpContext.RequestServices.GetRequiredService <UserManager <Models.ApplicationUser> >();
            SignInManager <Models.ApplicationUser> signinManager = context.HttpContext.RequestServices.GetRequiredService <SignInManager <Models.ApplicationUser> >();
            RoleManager <IdentityRole>             roleManager   = context.HttpContext.RequestServices.GetRequiredService <RoleManager <IdentityRole> >();
            ILoggerFactory logger = context.HttpContext.RequestServices.GetRequiredService <ILoggerFactory>();
            IMemoryCache   cache  = context.HttpContext.RequestServices.GetRequiredService <IMemoryCache>();

            Utilities.IAuthUtils authutils = context.HttpContext.RequestServices.GetRequiredService <Utilities.IAuthUtils>();
            RedditSharp.RefreshTokenWebAgentPool agentPool = context.HttpContext.RequestServices.GetRequiredService <RedditSharp.RefreshTokenWebAgentPool>();
            await agentPool.RemoveWebAgentAsync(context.Principal.Identity.Name);

            var user = await userManager.FindByNameAsync(context.Principal.Identity.Name);

            await authutils.UpdateModeratedSubredditsAsync(user);

            user = await userManager.FindByNameAsync(context.Principal.Identity.Name);

            var newPrincipal = await signinManager.CreateUserPrincipalAsync(user);

            if (user.HasWiki)
            {
                ((ClaimsIdentity)newPrincipal.Identity).AddClaim(new Claim("uri:snoonotes:haswiki", "true"));
            }
            if (user.HasConfig)
            {
                ((ClaimsIdentity)newPrincipal.Identity).AddClaim(new Claim("uri:snoonotes:hasconfig", "true"));
            }

            ((ClaimsIdentity)newPrincipal.Identity).AddClaim(new Claim("lastupdated", DateTime.UtcNow.ToString()));

            context.Principal = newPrincipal;
        }
예제 #2
0
        public async Task <List <string> > UpdateModeratedSubreddits()
        {
            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            await authUtils.UpdateModeratedSubredditsAsync(user);

            //search again for user to make sure it pulls claims correctly especially if using claims attached to a specific Role
            user = await _userManager.FindByNameAsync(User.Identity.Name);

            await _signInManager.SignInAsync(user, true, authenticationMethod : "cookie");

            return(user.Claims.Where(c => c.ClaimType == (User.Identity as ClaimsIdentity).RoleClaimType).ToList().Select(c => c.ClaimValue).ToList <string>());
        }
예제 #3
0
        public static async Task ValidateAsync(CookieValidatePrincipalContext context)
        {
            string lastUpdated = context.Principal.Claims.FirstOrDefault(c => c.Type == "lastupdated")?.Value;

            if (lastUpdated == null)
            {
                ((ClaimsIdentity)context.Principal.Identity).AddClaim(new Claim("lastupdated", DateTime.UtcNow.ToString()));
                context.ShouldRenew = true;
            }
            else if (DateTime.Parse(lastUpdated).AddHours(1) < DateTime.UtcNow)
            {
                //IConfigurationRoot config = context.HttpContext.RequestServices.GetRequiredService<IConfigurationRoot>();
                UserManager <Models.ApplicationUser>   userManager   = context.HttpContext.RequestServices.GetRequiredService <UserManager <Models.ApplicationUser> >();
                SignInManager <Models.ApplicationUser> signinManager = context.HttpContext.RequestServices.GetRequiredService <SignInManager <Models.ApplicationUser> >();
                RoleManager <IdentityRole>             roleManager   = context.HttpContext.RequestServices.GetRequiredService <RoleManager <IdentityRole> >();
                ILoggerFactory       logger    = context.HttpContext.RequestServices.GetRequiredService <ILoggerFactory>();
                IMemoryCache         cache     = context.HttpContext.RequestServices.GetRequiredService <IMemoryCache>();
                Utilities.IAuthUtils authutils = context.HttpContext.RequestServices.GetRequiredService <Utilities.IAuthUtils>();
                var user = await userManager.FindByNameAsync(context.Principal.Identity.Name);

                await authutils.UpdateModeratedSubredditsAsync(user);

                user = await userManager.FindByNameAsync(context.Principal.Identity.Name);

                var newPrincipal = await signinManager.CreateUserPrincipalAsync(user);

                if (user.HasWiki)
                {
                    ((ClaimsIdentity)newPrincipal.Identity).AddClaim(new Claim("uri:snoonotes:haswiki", "true"));
                }
                if (user.HasConfig)
                {
                    ((ClaimsIdentity)newPrincipal.Identity).AddClaim(new Claim("uri:snoonotes:hasconfig", "true"));
                }
                ((ClaimsIdentity)newPrincipal.Identity).AddClaim(new Claim("lastupdated", DateTime.UtcNow.ToString()));
                context.ReplacePrincipal(newPrincipal);
                context.ShouldRenew = true;
            }
        }