Beispiel #1
0
        private async Task <string> GetToken(AllergoUser user)
        {
            var utcNow = DateTime.UtcNow;

            var claims = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.UniqueName, user.UserName),
                new Claim(JwtRegisteredClaimNames.Jti, user.Id.ToString()),
                new Claim(JwtRegisteredClaimNames.Iat, utcNow.ToString(CultureInfo.InvariantCulture))
            };

            var userRoles = await _userManager.GetRolesAsync(user);

            foreach (var role in userRoles)
            {
                claims.Add(new Claim("role", role));
            }

            var signingKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration.GetValue <string>("Tokens:Key")));
            var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256);

            var jwt = new JwtSecurityToken(
                signingCredentials: signingCredentials,
                claims: claims,
                notBefore: utcNow,
                expires: utcNow.AddSeconds(_configuration.GetValue <int>("Tokens:Lifetime"))
                );

            return(new JwtSecurityTokenHandler().WriteToken(jwt));
        }
Beispiel #2
0
        public async Task <string> Register(RegisterViewModel model)
        {
            _authValidationService.ValidateRegisterViewModel(model);

            var newUser = new AllergoUser
            {
                UserName  = model.UserName,
                Email     = model.Email,
                FirstName = model.FirstName,
                LastName  = model.LastName
            };

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

            await _userManager.AddToRoleAsync(newUser, AllergoRoleNames.Patient);

            if (!result.Succeeded)
            {
                throw new BadRequestException(
                          $"An error occured while registering user: {result.Errors.Select(x => x.Description).Join()}");
            }

            await _signInManager.SignInAsync(newUser, false);

            return(await GetToken(newUser));
        }
        private static void AddIfNotExists(AllergoUser user, UserManager <AllergoUser> userManager)
        {
            if (userManager.FindByNameAsync(user.UserName).Result == null)
            {
                IdentityResult result = userManager.CreateAsync(user, "Haslo123.").Result;

                if (result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, user.UserName.ToUpper()).Wait();
                }
            }
        }
Beispiel #4
0
        private async Task SetRoleForUserAsync(AllergoUser user, string roleId)
        {
            var role = await _roleManager.FindByIdAsync(roleId);

            if (await _userManager.IsInRoleAsync(user, role.Name))
            {
                return;
            }

            var currentRoles = await _userManager.GetRolesAsync(user);

            await _userManager.RemoveFromRolesAsync(user, currentRoles);

            await _userManager.AddToRoleAsync(user, role.Name);
        }
        public static void SeedUsers(UserManager <AllergoUser> userManager)
        {
            var adminUser = new AllergoUser
            {
                UserName           = "******",
                NormalizedUserName = "******",
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**",
                FirstName       = "Admin",
                LastName        = "Adminowski",
            };

            var doctorUser = new AllergoUser
            {
                UserName           = "******",
                NormalizedUserName = "******",
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**",
                FirstName       = "Doktor",
                LastName        = "Doktorski",
            };

            var patientUser = new AllergoUser
            {
                UserName           = "******",
                NormalizedUserName = "******",
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**",
                FirstName       = "Pacjent",
                LastName        = "Cierpliwy",
            };

            AddIfNotExists(adminUser, userManager);
            AddIfNotExists(doctorUser, userManager);
            AddIfNotExists(patientUser, userManager);
        }