Exemplo n.º 1
0
        public UserLogin UserLogin(User user)
        {
            User account = DbUser.FirstOrDefault(x => x.Email == user.Email);

            if (account == null || !BCrypt.Net.BCrypt.Verify(user.Password, account.Password))
            {
                return(null);
            }

            var authParams  = TokenOption.Value;
            var securityKey = authParams.GetSymmetricSecurityKey();
            var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

            var claims = new List <Claim>()
            {
                new Claim(JwtRegisteredClaimNames.Email, account.Email),
                new Claim(JwtRegisteredClaimNames.Sub, account.Id.ToString())
            };

            var token = new JwtSecurityToken(authParams.Issuer,
                                             authParams.Audience,
                                             claims,
                                             expires: DateTime.Now.AddSeconds(authParams.TokenLifeTime),
                                             signingCredentials: credentials);

            var tokenHandler = new JwtSecurityTokenHandler().WriteToken(token);

            return(new UserLogin(tokenHandler, account.Id));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get an entity if already existing or add it and return it's context if it doesn't already exist.
        /// </summary>
        /// <typeparam name="TEntity">Type of the data entity.</typeparam>
        /// <param name="set">The databsae set to perform the query on.</param>
        /// <param name="entity">The entity to perform the query on.</param>
        /// <param name="identifierFunction">How the entity is unique.</param>
        /// <returns>The instance of the processed entity.</returns>
        public static TEntity GetOrAdd <TEntity>(this Microsoft.EntityFrameworkCore.DbSet <TEntity> set, TEntity entity, Func <TEntity, bool> identifierFunction) where TEntity : class
        {
            // Get the existing entity if any, else get null
            TEntity existingEntity = set
                                     .FirstOrDefault(identifierFunction);

            // If there is an existing entity, return it
            if (existingEntity != null)
            {
                return(existingEntity);
            }

            // If no entity of this kind exists, create it and return it
            set.Add(entity);

            return(entity);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Get first entity for the specified predicate.
 /// </summary>
 /// <param name="predicate">The predicate.</param>
 /// <returns></returns>
 public T FirstOrDefault(Expression <Func <T, bool> > predicate)
 {
     return(_dbSet.FirstOrDefault(predicate));
 }