Exemple #1
0
        public AppUserDm InsertUser(AppUserDm user)
        {
            PiContext.ApplicationUsers.Add(user);
            PiContext.SaveChanges();

            return(user);
        }
Exemple #2
0
        private Task <ClaimsIdentity> CreateClaimsIdentities(AppUserDm user, IEnumerable <string> roles)
        {
            ClaimsIdentity claimsIdentity = new ClaimsIdentity();

            claimsIdentity.AddClaim(new Claim(ClaimTypes.Email, user.Email));
            claimsIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
            claimsIdentity.AddClaim(new Claim(ClaimTypes.Name, user.Username));
            claimsIdentity.AddClaim(new Claim(ClaimTypes.GivenName, user.DisplayName));
            claimsIdentity.AddClaim(new Claim(ClaimTypes.UserData, JsonConvert.SerializeObject(new { DisplayName = user.DisplayName })));

            foreach (var role in roles)
            {
                claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, role));
            }

            return(Task.FromResult(claimsIdentity));
        }
Exemple #3
0
        public void CreateUser(string username, string password)
        {
            var user = new AppUserDm()
            {
                ActiveFrom  = DateTime.Now,
                ActiveTo    = DateTime.Now.AddYears(100),
                CreatedDate = DateTime.Now,
                DisplayName = username,
                Username    = username
            };
            var hashedPassword = passwordHashing.HashPassword(password);

            user.Password = hashedPassword.Password;
            user.Salt     = hashedPassword.Salt;

            userRepository.InsertUser(user);
        }
Exemple #4
0
        public async Task <string> CreateJwtAsync(
            AppUserDm user,
            IEnumerable <string> roles,
            int daysValid)
        {
            var tokenHandler = new JwtSecurityTokenHandler();
            var claims       = await CreateClaimsIdentities(user, roles);

            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(apiConfig.PrivateKey));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var token = tokenHandler.CreateJwtSecurityToken(
                issuer: apiConfig.Issuer,
                audience: apiConfig.Issuer,
                subject: claims,
                notBefore: DateTime.UtcNow,
                expires: DateTime.UtcNow.AddDays(daysValid),
                signingCredentials: creds);

            return(tokenHandler.WriteToken(token));
        }