コード例 #1
0
        public string Authenticate([FromBody] LoginModel model)
        {
            var username       = model.UserName;
            var password       = model.Password;
            var salt           = _context.User.SingleOrDefault(x => x.UserName == username)?.PasswordSalt;
            var hashedPassword = PasswordHashGenerator.ComputeHash(password, new SHA256CryptoServiceProvider(), PasswordHashGenerator.StringToByteArray(salt));
            var user           = _context.User.SingleOrDefault(x => x.UserName == username && x.PasswordHash == hashedPassword);

            // return null if user not found
            if (user == null)
            {
                return(null);
            }

            // authentication successful so generate jwt token
            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(this._appSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, user.UserId.ToString())
                }),
                Expires            = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var stringToken = tokenHandler.WriteToken(token);

            return(stringToken);
        }
コード例 #2
0
        public static void Seed(IServiceProvider serviceProvider)
        {
            using (var context =
                       new CityVisitTrackerAPIContext(serviceProvider
                                                      .GetRequiredService <DbContextOptions <CityVisitTrackerAPIContext> >()))
            {
                // If there is a state that assuming that we have already seeded some data and we will skip this on startup
                // Seed State, Cities, Users and UserVisits
                if (context.State.Any())
                {
                    return;
                }
                var alabama = new State
                {
                    Abbreviation = "AL",
                    DateAdded    = DateTime.Today,
                    LastUpdated  = DateTime.Today,
                    Name         = "Alabama",
                    Cities       = new List <City>()
                };
                var alaska = new State
                {
                    Abbreviation = "AK",
                    DateAdded    = DateTime.Today,
                    LastUpdated  = DateTime.Today,
                    Name         = "Alaska",
                    Cities       = new List <City>()
                };
                var arizona = new State
                {
                    Abbreviation = "AZ",
                    DateAdded    = DateTime.Today,
                    LastUpdated  = DateTime.Today,
                    Name         = "Arizona",
                    Cities       = new List <City>()
                };

                var seedStates = new[] { alabama, alaska, arizona };
                context.State.AddRange(seedStates);
                context.SaveChanges();
                var alabamaStateId = context.State.FirstOrDefaultAsync(x => x.Name == "Alabama").Id;
                var alaskaStateId  = context.State.FirstOrDefaultAsync(x => x.Name == "Alaska").Id;
                var arizonaStateId = context.State.FirstOrDefaultAsync(x => x.Name == "Arizona").Id;
                var seedCities     = new[] {
                    AddCity("Akron", alabamaStateId, 32.87802m, -87.743989m),
                    AddCity("Huntsville", alabamaStateId, 34.729135m, -86.584979m),
                    AddCity("Addison", alabamaStateId, 32.87802m, -87.181384m),
                    AddCity("Montgomery", alabamaStateId, 34.202175m, -86.300629m),
                    AddCity("Birmingham", alabamaStateId, 32.38012m, -86.811504m),

                    AddCity("Adak", alaskaStateId, 51.88001m, -176.657569m),
                    AddCity("Akhiok", alaskaStateId, 56.945599m, -154.169998m),
                    AddCity("Akiak", alaskaStateId, 60.909659m, -161.223451m),
                    AddCity("Kasigluk", alaskaStateId, 60.895273m, -162.517124m),
                    AddCity("Akutan", alaskaStateId, 54.134725m, -165.770554m),

                    AddCity("Mesa", arizonaStateId, 33.417045m, -111.831459m),
                    AddCity("Phoenix", arizonaStateId, 33.44826m, -112.075774m),
                    AddCity("Avondale", arizonaStateId, 33.4405m, -112.349664m),
                    AddCity("Mohave Valley", arizonaStateId, 34.92384m, -114.597859m),
                    AddCity("Whiteriver", arizonaStateId, 33.834865m, -109.964934m),
                };
                context.City.AddRange(seedCities);
                context.SaveChanges();
                var salt           = PasswordHashGenerator.GetSalt();
                var hashedPassword = PasswordHashGenerator.ComputeHash("TestPassword", new SHA256CryptoServiceProvider(), salt);
                var user           = new User
                {
                    FirstName    = "Sachin",
                    LastName     = "Patel",
                    DateAdded    = DateTime.Today,
                    LastUpdated  = DateTime.Today,
                    UserName     = "******",
                    PasswordSalt = PasswordHashGenerator.GetSaltString(salt),
                    PasswordHash = hashedPassword
                };
                context.User.Add(user);
                context.SaveChanges();
            }
        }