Example #1
0
        public async Task SeedAsync()
        {
            var email    = "*****@*****.**";
            var password = "******";
            var name     = "Filipe MacĂȘdo";

            var user = await _userManager.FindByEmailAsync(email);

            if (user == null)
            {
                user = new CoreIdentityUser {
                    UserName = email, Email = email, Name = name
                };

                var userResult = await _userManager.CreateAsync(user, password);

                if (!userResult.Succeeded)
                {
                    throw new InvalidOperationException(userResult.Errors.Select(e => e.Description).Aggregate((a, b) => a + " - " + b));
                }

                var claimResult = await _userManager.AddClaimAsync(user, TodoListClaims.Roles.Common);

                if (!claimResult.Succeeded)
                {
                    throw new InvalidOperationException(userResult.Errors.Select(e => e.Description).Aggregate((a, b) => a + " - " + b));
                }

                await _context.SaveChangesAsync();
            }
        }
Example #2
0
        public async Task <string> Build(CoreIdentityUser user, TokenProviderOptions options)
        {
            var now = DateTime.UtcNow;

            var claims = new Claim[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
                new Claim(JwtRegisteredClaimNames.Jti, await options.NonceGenerator()),
                new Claim(JwtRegisteredClaimNames.Iat, now.ToUnixEpochDate().ToString(), ClaimValueTypes.Integer64),
                new Claim("IdentityUserId", user.Id)
            };

            claims.Union(user.Claims.Select(o => new Claim(o.ClaimType, o.ClaimValue)));

            // Create the JWT and write it to a string
            var jwt = new JwtSecurityToken(
                claims: claims,
                notBefore: now,
                expires: now.Add(options.Expiration),
                signingCredentials: options.SigningCredentials);
            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

            return(encodedJwt);
        }