Ejemplo n.º 1
0
        private static IReadOnlyCollection <Claim> GetClaims(string username, string password)
        {
            using (var context = new PaperWorkerDbContext())
            {
                var user = context.Users
                           .Include(x => x.Roles)
                           .ThenInclude(x => x.Role)
                           .SingleOrDefault(x => x.Username == username);

                if (user == null)
                {
                    return(null);
                }

                var sha256       = new SHA256Managed();
                var passwordHash = Convert.ToBase64String(sha256.ComputeHash(Encoding.UTF8.GetBytes(password)));
                if (passwordHash != user.Password)
                {
                    return(null);
                }

                var claims = user.Roles
                             .Select(userRole => new Claim(ClaimsIdentity.DefaultRoleClaimType, userRole.Role.Name.ToString()))
                             .ToList();

                claims.Add(new Claim(ClaimsIdentity.DefaultNameClaimType, user.Username));

                return(claims);
            }
        }
Ejemplo n.º 2
0
 public IActionResult Get()
 {
     using (var context = new PaperWorkerDbContext())
     {
         return(Ok(context.Roles.Select(role => new Role
         {
             Id = role.Id,
             Name = role.Name
         }).ToList()));
     }
 }
Ejemplo n.º 3
0
 public IActionResult Get()
 {
     using (var context = new PaperWorkerDbContext())
     {
         return(Ok(context.Users.Select(user => new User
         {
             Id = user.Id,
             Username = user.Username,
             Password = user.Password
         }).ToList()));
     }
 }
Ejemplo n.º 4
0
        private static async Task CreateRoles()
        {
            using (var context = new PaperWorkerDbContext())
            {
                var roleNames = Enum.GetValues(typeof(RoleName)).Cast <RoleName>();
                foreach (var roleName in roleNames)
                {
                    if (await context.ExistsRole(roleName))
                    {
                        continue;
                    }

                    await context.AddRole(new Role { Name = roleName });
                }
            }
        }
Ejemplo n.º 5
0
 public static async Task AddUser(this PaperWorkerDbContext context, User user)
 {
     context.Users.Add(user);
     await context.SaveChangesAsync();
 }
Ejemplo n.º 6
0
 public static User GetUser(this PaperWorkerDbContext context, string username)
 {
     return(context.Users.SingleOrDefault(x => x.Username == username));
 }
Ejemplo n.º 7
0
 public static async Task AddRole(this PaperWorkerDbContext context, Role role)
 {
     context.Roles.Add(role);
     await context.SaveChangesAsync();
 }
Ejemplo n.º 8
0
 public static async Task <bool> ExistsRole(this PaperWorkerDbContext context, RoleName roleName)
 {
     return(await context.Roles.AnyAsync(role => role.Name == roleName));
 }