public async Task <TicketScalperToken> GenerateForUser(TicketScalperIdentityUser user, string optionsKey = "TokenOptions")
        {
            _config.Bind(optionsKey, _tokenOptions);

            // Create the token
            var claims = new List <Claim>()
            {
                new Claim(JwtRegisteredClaimNames.Sub, user.Email),
                new Claim(JwtRegisteredClaimNames.Jti, user.Id),
                new Claim(JwtRegisteredClaimNames.UniqueName, user.UserName)
            };

            var userClaims = await _userManager.GetClaimsAsync(user);

            claims.AddRange(userClaims);

            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_tokenOptions.SigningKey));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512);

            var token = new JwtSecurityToken(
                _tokenOptions.Issuer,
                _tokenOptions.Audience,
                claims,
                expires: DateTime.Now.AddMinutes(_tokenOptions.ExpirationLength),
                signingCredentials: creds);

            return(new TicketScalperToken()
            {
                Token = new JwtSecurityTokenHandler().WriteToken(token),
                Expiration = token.ValidTo
            });
        }
示例#2
0
        public async Task <ActionResult <TicketScalperIdentityModel> > Post([FromBody] TicketScalperIdentityRequestModel model)
        {
            try
            {
                var user = new TicketScalperIdentityUser()
                {
                    UserName       = model.Username,
                    Email          = model.Username,
                    EmailConfirmed = true
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    return(CreatedAtRoute(new { username = user.UserName }, _mapper.Map <TicketScalperIdentityModel>(user)));
                }
                else
                {
                    return(BadRequest(result.Errors.FirstOrDefault()?.Description));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to create new login: "******"Failed to create user: {ex.Message}"));
            }
        }
示例#3
0
        public static async Task Seed()
        {
            using (var scope = GenerateServiceScope())
            {
                var svcs = scope.ServiceProvider;
                var db   = svcs.GetService <TicketScalperIdentityContext>();
                db.Database.EnsureCreated();

                Console.WriteLine("Database Created");


                var userManager = svcs.GetService <UserManager <TicketScalperIdentityUser> >();

                var first = await userManager.FindByEmailAsync("*****@*****.**");

                if (first == null)
                {
                    first = new TicketScalperIdentityUser()
                    {
                        UserName       = "******",
                        Email          = "*****@*****.**",
                        EmailConfirmed = true
                    };

                    if ((await userManager.CreateAsync(first, "P@ssw0rd!")) == IdentityResult.Success)
                    {
                        // Add claim
                        await userManager.AddClaimsAsync(first, new Claim[]
                        {
                            new Claim(JwtRegisteredClaimNames.GivenName, "Shawn"),
                            new Claim(JwtRegisteredClaimNames.FamilyName, "Wildermuth"),
                            new Claim("role", "user"),
                            new Claim("role", "admin")
                        });

                        Console.WriteLine($"User ({first.UserName}) created.");
                    }
                    else
                    {
                        Console.WriteLine("Failed to create user");
                    }
                }

                var second = await userManager.FindByEmailAsync("*****@*****.**");

                if (second == null)
                {
                    second = new TicketScalperIdentityUser()
                    {
                        UserName       = "******",
                        Email          = "*****@*****.**",
                        EmailConfirmed = true
                    };

                    if ((await userManager.CreateAsync(second, "P@ssw0rd!")) == IdentityResult.Success)
                    {
                        // Add claims
                        await userManager.AddClaimsAsync(second, new Claim[]
                        {
                            new Claim(JwtRegisteredClaimNames.GivenName, "Resa"),
                            new Claim(JwtRegisteredClaimNames.FamilyName, "Wildermuth"),
                            new Claim("role", "user")
                        });

                        Console.WriteLine($"User ({second.UserName}) created.");
                    }
                    else
                    {
                        Console.WriteLine("Failed to create user");
                    }
                }
            };
            Console.WriteLine("Database seeded...");
        }