Exemple #1
0
        public async Task <List <TEntity> > GetAllByFilter(Expression <Func <TEntity, bool> > filter)
        {
            using var context = new JwtContext();
            return(await context.Set <TEntity>().Where(filter).ToListAsync());

            //throw new NotImplementedException();
        }
Exemple #2
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var authHeader = context.HttpContext.Request.GetHeader("Authorization");

            if (authHeader == null || !authHeader.StartsWith("Bearer "))
            {
                throw new Exception("Authorization Bearer token is missing");
            }

            string token = authHeader.Replace("Bearer", "").Trim();

            JwtContext jwtContext = null;

            try
            {
                jwtContext = Decode(token);
            }
            catch (Exception)
            {
                throw new UnauthorizedAccessException("Invalid token");
            }

            if (jwtContext != null && _roles.Contains(jwtContext.UserRole))
            {
                base.OnActionExecuting(context);
            }
            else
            {
                throw new UnauthorizedAccessException("Unauthorized");
            }
        }
Exemple #3
0
        public async Task <TEntity> GetByFilter(Expression <Func <TEntity, bool> > filter)
        {
            using var context = new JwtContext();
            return(await context.Set <TEntity>().FirstOrDefaultAsync(filter));

            //throw new NotImplementedException();
        }
Exemple #4
0
        public async Task <TEntity> GetById(int id)
        {
            using var context = new JwtContext();
            return(await context.Set <TEntity>().FindAsync(id));

            // throw new NotImplementedException();
        }
Exemple #5
0
        public async Task Update(TEntity entity)
        {
            var context = new JwtContext();

            context.Update(entity);
            await context.SaveChangesAsync();
        }
Exemple #6
0
        public async Task <List <TEntity> > GetAll()
        {
            //throw new NotImplementedException();

            using var context = new JwtContext();
            return(await context.Set <TEntity>().ToListAsync());
        }
Exemple #7
0
        public async Task <bool> CheckPasswordAsync(AppUserLoginDto appUserLoginDto)
        {
            var context = new JwtContext();
            var appUser = await context.AppUsers.Where(x => x.UserName == appUserLoginDto.UserName).FirstOrDefaultAsync();

            return(appUser.Password == appUserLoginDto.Password ? true : false);
        }
Exemple #8
0
 public async Task Add(TEntity entity)
 {
     using (var context = new JwtContext())
     {
         context.Add(entity);
         await context.SaveChangesAsync();
     }
 }
Exemple #9
0
        public async Task Update(TEntity entity)
        {
            using var context = new JwtContext();
            context.Update(entity);
            await context.SaveChangesAsync();

            // throw new NotImplementedException();
        }
Exemple #10
0
        public static string Encode(JwtContext context)
        {
            IJwtAlgorithm     algorithm  = new HMACSHA256Algorithm();
            IJsonSerializer   serializer = new JsonNetSerializer();
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtEncoder       encoder    = new JwtEncoder(algorithm, serializer, urlEncoder);

            return(encoder.Encode(context, Constants.JWT_SECRET));
        }
Exemple #11
0
        public static JwtContext ContextFromUser(User user)
        {
            JwtContext context = new JwtContext();

            context.UserId   = user.Id;
            context.UserRole = user.Role;
            context.Expiry   = DateTime.Now.AddMinutes(Constants.JWT_SESSION_LENGHT);

            return(context);
        }
Exemple #12
0
 public async Task <List <AppRole> > GetRolesByUserName(string userName)
 {
     using var context = new JwtContext();
     return(await context.AppUsers.Join(context.AppUserRoles, u => u.Id, ur => ur.AppUserId, (user, userRole) => new
     {
         user = user,
         userRole = userRole
     }).Join(context.AppRoles, two => two.userRole.AppRoleId, r => r.Id, (twoTable, role) => new
     {
         user = twoTable.user,
         userRole = twoTable.userRole,
         role = role
     }).Where(I => I.user.UserName == userName).Select(I => new AppRole
     {
         Id = I.role.Id,
         Name = I.role.Name
     }).ToListAsync());
 }
 public async Task <T> GetById(int Id)
 {
     using var context = new JwtContext();
     return(await context.Set <T>().FindAsync(Id));
 }
 public async Task <T> GetByFilter(Expression <Func <T, bool> > filter)
 {
     using var context = new JwtContext();
     return(await context.Set <T>().FirstOrDefaultAsync(filter));
 }
 public async Task <List <T> > GetAllByFilter(Expression <Func <T, bool> > filter)
 {
     using var context = new JwtContext();
     return(await context.Set <T>().Where(filter).ToListAsync());
 }
 public async Task <List <T> > GetAll()
 {
     using var context = new JwtContext();
     return(await context.Set <T>().ToListAsync());
 }
 public UnitOfWork(JwtContext context)
 {
     _context = context;
     User     = new UserRepository(_context);
     Token    = new Token(_context);
 }
Exemple #18
0
 public async Task Add(TEntity entity)
 {
     using var context = new JwtContext();
     context.Set <TEntity>().Add(entity);
     await context.SaveChangesAsync();
 }
Exemple #19
0
        public async Task <TEntity> GetById(int id)
        {
            var context = new JwtContext();

            return(await context.Set <TEntity>().FindAsync(id));
        }
Exemple #20
0
        public async Task <TEntity> GetByFilter(Expression <Func <TEntity, bool> > filter)
        {
            var context = new JwtContext();

            return(await context.Set <TEntity>().Where(filter).FirstOrDefaultAsync());
        }
Exemple #21
0
        public async Task <AppUser> FindByUserNameAsync(string userName)
        {
            var context = new JwtContext();

            return(await context.AppUsers.Where(x => x.UserName == userName).FirstOrDefaultAsync());
        }
Exemple #22
0
 public Token(JwtContext context)
 {
     _context = context;
 }
 public async Task Remove(T entity)
 {
     using var context = new JwtContext();
     context.Remove(entity);
     await context.SaveChangesAsync();
 }
        public async Task <AppRole> FindByNameAsync(string roleName)
        {
            var contex = new JwtContext();

            return(await contex.AppRoles.Where(x => x.Name == roleName).FirstOrDefaultAsync());
        }
 public UserRepository(JwtContext context)
 {
     _context = context;
 }
Exemple #26
0
 public AccountController(JwtContext context, IOptions <AuthOptions> options, IHostEnvironment hostEnvironment)
 {
     this.context         = context;
     this.options         = options;
     this.hostEnvironment = hostEnvironment;
 }
Exemple #27
0
 public async Task Delete(int id)
 {
     using var context = new JwtContext();
     context.Remove(id);
     await context.SaveChangesAsync();
 }