public void SeedClientApplicationData(ModelBuilder modelBuilder)
        {
            foreach (var clientApplication in ClientApplications)
            {
                var salt         = HashString.GetSalt();
                var hashPassword = HashString.Hash(clientApplication.ClientApplicationPassword, salt
                                                   , AuthorizationUtilConstants.IterationCountForHashing);
                var clientApplicationUtil = new ClientApplicationUtil()
                {
                    Id = ClientApplicationUtilId,
                    ClientApplicationId = clientApplication.Id,
                    SpecialValue        = salt,
                };

                clientApplication.ClientApplicationPassword = hashPassword;

                SeedDataUtil.SetCommonFields <ClientApplication, int>(clientApplication, LoggedUserId);
                SeedDataUtil.SetCommonFields <ClientApplicationUtil, int>(clientApplicationUtil, LoggedUserId);

                modelBuilder.Entity <ClientApplication>()
                .HasData(clientApplication);

                modelBuilder.Entity <ClientApplicationUtil>()
                .HasData(clientApplicationUtil);

                ClientApplicationUtilId++;
            }
        }
Exemple #2
0
        public async Task <JsonResult> Login([FromBody] LoginVM loginUser)
        {
            //hash the password
            string password = HashString.Hash(loginUser.password, config["salt"]);

            var user = await context.Users
                       .Where(u => u.username == loginUser.username)
                       .Where(u => u.hashedPassword == password)
                       .FirstOrDefaultAsync();

            if (user != null)
            {
                //Generate the JWT
                GenerateJWT jwtGen = new GenerateJWT();
                TokenVM     token  = jwtGen.Generate(user.username, config);
                return(Json(new JSONTokenResponseVM {
                    message = "Successfully logged in", token = token.token
                }));
            }
            else
            {
                return(Json(new JSONResponseVM {
                    success = false, message = "Incorrect login details"
                }));
            }
        }
        public void SeedUserData(ModelBuilder modelBuilder)
        {
            foreach (var user in Users)
            {
                var salt         = HashString.GetSalt();
                var hashPassword = HashString.Hash(user.Password, salt
                                                   , AuthorizationUtilConstants.IterationCountForHashing);

                var userUtil = new UserUtil
                {
                    Id           = UserUtilId,
                    UserId       = user.Id,
                    SpecialValue = salt,
                };

                user.Password = hashPassword;

                SeedDataUtil.SetCommonFields <User, int>(user, LoggedUserId);
                SeedDataUtil.SetCommonFields <UserUtil, int>(userUtil, LoggedUserId);

                modelBuilder.Entity <User>()
                .HasData(user);

                modelBuilder.Entity <UserUtil>()
                .HasData(userUtil);

                UserUtilId++;
            }
        }
Exemple #4
0
        public Task <ClientApplication> CreateAsync(ClientApplicationRequest request)
        {
            return(CommonOperationAsync(async() =>
            {
                var result = Mapper.Map <ClientApplication>(request);

                var tempResult = await _uow.ClientApplications.GetByNameAsync(result.ClientApplicationName);
                tempResult.CheckUniqueValue(AuthorizationConstants.ClientApplicationName);

                tempResult = await _uow.ClientApplications.GetByCodeAsync(result.ClientApplicationCode);
                tempResult.CheckUniqueValue(AuthorizationConstants.ClientApplicationCode);

                var salt = HashString.GetSalt();
                var hashPassword = HashString.Hash(result.ClientApplicationPassword, salt,
                                                   AuthorizationUtilConstants.IterationCountForHashing);

                result.ClientApplicationPassword = hashPassword;

                _uow.ClientApplications.Add(result, GetLoggedInUserId());

                CreateClientApplicationUtil(result.Id, salt);

                await _uow.SaveChangesAsync();
                return result;
            }, new BusinessBaseRequest {
                MethodBase = MethodBase.GetCurrentMethod()
            }));
        }
Exemple #5
0
        public Task <ClientApplication> LoginAsync(string code, string password)
        {
            return(CommonOperationAsync(async() =>
            {
                ClientApplication clientApplication;
                try
                {
                    clientApplication = await GetByClientApplicationCodeAsync(code);
                }
                catch (KeyNotFoundException) //Eğer code sistemde yoksa, kayıt bulunamadı yerine authentication hatası fırlatması için try - catch kullanılıyor
                {
                    throw new AuthenticationException();
                }

                var clientApplicationUtil =
                    await _uow.ClientApplicationUtils.GetByClientApplicationIdAsync(clientApplication.Id);

                password = HashString.Hash(password, clientApplicationUtil.SpecialValue,
                                           AuthorizationUtilConstants.IterationCountForHashing);

                var client = await _uow.ClientApplications.GetByCodeAndPasswordAsync(code, password);
                if (client == null)
                {
                    throw new AuthenticationException();
                }

                return client;
            }, new BusinessBaseRequest {
                MethodBase = MethodBase.GetCurrentMethod()
            }));
        }
        public Task <User> LoginAsync(string email, string password)
        {
            return(CommonOperationAsync(async() =>
            {
                var user = await _uow.Users.GetByEmailAsync(email);
                if (user == null)
                {
                    throw new AuthenticationException();
                }

                /** Hash password **/
                var userUtil = await _uow.UserUtils.GetByUserIdAsync(user.Id);
                var hashed = HashString.Hash(password, userUtil.SpecialValue,
                                             AuthorizationUtilConstants.IterationCountForHashing);
                password = hashed;
                /*******************/

                var result = await _uow.Users.GetByEmailAndPasswordAsync(email, password);
                if (result == null)
                {
                    throw new AuthenticationException();
                }

                return result;
            }, new BusinessBaseRequest {
                MethodBase = MethodBase.GetCurrentMethod()
            }));
        }
Exemple #7
0
        public async Task <JsonResult> CreateAccount([FromBody] CreateAccountVM account)
        {
            //validate

            if (account.username == null || account.password == null || account.password2 == null)
            {
                return(Json(new JSONResponseVM {
                    success = false, message = "Please fill out all the fields"
                }));
            }
            if (account.password != account.password2)
            {
                return(Json(new JSONResponseVM {
                    success = false, message = "Passwords don't match"
                }));
            }

            //check to see if this username already exists
            var query = await context.Users.Where(u => u.username == account.username).FirstOrDefaultAsync();

            if (query != null)
            {
                //this username exists
                return(Json(new JSONResponseVM {
                    success = false, message = "A user with this username already exists"
                }));
            }


            string newPassword = account.password;

            newPassword = HashString.Hash(newPassword, config["salt"]);

            //now add the new user to the database
            User newUser = new User
            {
                username       = account.username,
                hashedPassword = newPassword,
                //the user is by default a customer
                role_id = 0,
                role    = await context.Roles.Where(r => r.role_id == 0).FirstOrDefaultAsync()
            };

            context.Users.Add(newUser);
            context.SaveChanges();

            //generate the token
            GenerateJWT jwtGen = new GenerateJWT();
            TokenVM     tok    = jwtGen.Generate(newUser.username, config);

            return(Json(new JSONTokenResponseVM {
                message = "Successfully created account: " + newUser.username, token = tok.token
            }));
        }
        public Task <ClientApplication> UpdateClientApplicationPasswordAsync(int id, string clientApplicationPassword)
        {
            return(CommonOperationAsync(async() =>
            {
                var result = await GetByIdAsync(id);

                var salt = HashString.GetSalt();
                var hashPassword = HashString.Hash(clientApplicationPassword, salt,
                                                   IdentityUtilConstants.IterationCountForHashing);

                result.SecurityStamp = salt;
                result.ClientApplicationPassword = hashPassword;

                _uow.ClientApplications.Update(result, GetUserId());

                await _uow.SaveChangesAsync();
                return result;
            }, new BusinessBaseRequest {
                MethodBase = MethodBase.GetCurrentMethod()
            }));
        }
        public Task <User> UpdatePasswordAsync(int id, string password)
        {
            return(CommonOperationAsync(async() =>
            {
                var result = await GetByIdAsync(id);

                /** Hash password **/
                var salt = HashString.GetSalt();
                var hashPassword = HashString.Hash(password, salt, AuthorizationUtilConstants.IterationCountForHashing);

                result.Password = hashPassword;

                _uow.Users.Update(result, GetLoggedInUserId());

                await UpdateUserUtilAsync(id, salt);

                await _uow.SaveChangesAsync();
                return result;
            }, new BusinessBaseRequest {
                MethodBase = MethodBase.GetCurrentMethod()
            }));
        }
        public Task <ClientApplication> LoginAsync(string code, string password)
        {
            return(CommonOperationAsync(async() =>
            {
                var clientApplication = await _uow.ClientApplications.GetByCodeAsync(code);
                if (clientApplication == null)
                {
                    throw new AuthenticationException();
                }

                password = HashString.Hash(password, clientApplication.SecurityStamp,
                                           IdentityUtilConstants.IterationCountForHashing);

                var client = await _uow.ClientApplications.GetByCodeAndPasswordAsync(code, password);
                if (client == null)
                {
                    throw new AuthenticationException();
                }

                return client;
            }, new BusinessBaseRequest {
                MethodBase = MethodBase.GetCurrentMethod()
            }));
        }