Esempio n. 1
0
        public async Task <IActionResult> Register(string userName, string password, string name, string lastName)
        {
            var user = new ApplicationUser
            {
                Name     = name,
                Access   = "Customer",
                LastName = lastName,
                UserName = userName,
            };

            var result = await userManager.CreateAsync(user, password);

            if (result.Succeeded)
            {
                user = await userManager.FindByNameAsync(user.UserName);

                await authDbContext.AddRangeAsync();

                await authDbContext.SaveChangesAsync();

                var cusInfo = new cusInfo
                {
                    AppUserID = user.Id,
                };

                await authDbContext.AddAsync(cusInfo);

                await authDbContext.SaveChangesAsync();
            }

            return(RedirectToAction("Index", "Customer", new { area = "" }));
        }
Esempio n. 2
0
        public async Task <ApiResponse> CreateUser(CreateUserRequestModel req)
        {
            User user = new();

            user.Email               = req.Email;
            user.Username            = req.Username;
            user.IsActive            = true;
            user.IsEmailVerification = false;

            if (req.IsHash)
            {
                user.PasswordHash = req.Password;
            }
            else
            {
                user.Password = req.Password;
            }

            await _context.AddAsync(user);

            await _context.SaveChangesAsync();

            return(new ApiResponse(
                       "Kullanıcı başarı ile oluşturuldu.",
                       new CreateUserResponseModel
            {
                Email = user.Email,
                Id = user.Id
            }));
        }
Esempio n. 3
0
        /// <summary>
        /// Adds an user with role
        /// </summary>
        /// <param name="username">Username</param>
        /// <param name="password">Password</param>
        /// <param name="email">Email</param>
        /// <param name="role">Role</param>
        /// <param name="apiKey">API Key</param>
        /// <returns></returns>
        public async Task <Users> AddUserAndRoleAsync(string username, string password, string email, string role, string apiKey)
        {
            if (apiKey != JwtOptions.ApiKey && IsInProduction)
            {
                throw new ArgumentException("Invalid API Key.");
            }

            var dbRole = await AuthDbContext.Roles.SingleOrDefaultAsync(r => r.Role == role);

            if (dbRole == null)
            {
                throw new ArgumentException("Invalid role");
            }

            var user = await AddUserAsync(username, password, email, apiKey);

            var newUserRole = new UsersRoles()
            {
                UsersRoleId = Guid.NewGuid(),
                UserId      = user.UserId,
                RoleId      = dbRole.RoleId
            };

            await AuthDbContext.AddAsync(newUserRole);

            await AuthDbContext.SaveChangesAsync();

            return(user);
        }
Esempio n. 4
0
        /// <summary>
        /// Adds a role
        /// </summary>
        /// <param name="role">Role</param>
        /// <param name="isAdmin">Indicates if this role is an Admin role</param>
        /// <param name="apiKey">API Key</param>
        /// <returns></returns>
        public async Task <Roles> AddRole(string role, bool isAdmin, string apiKey)
        {
            if (apiKey != JwtOptions.ApiKey && IsInProduction)
            {
                throw new ArgumentException("Invalid API Key.");
            }

            var roleExists = await AuthDbContext.Roles
                             .AnyAsync(r => r.Role == role);

            if (roleExists)
            {
                throw new ArgumentException("Role already exists");
            }

            Roles newRole = new Roles()
            {
                RoleId  = Guid.NewGuid(),
                Role    = role,
                IsAdmin = isAdmin
            };

            await AuthDbContext.AddAsync(newRole);

            await AuthDbContext.SaveChangesAsync();

            return(newRole);
        }
Esempio n. 5
0
        /// <summary>
        /// Adds an user
        /// </summary>
        /// <param name="username">Username</param>
        /// <param name="password">Password</param>
        /// <param name="email">Email</param>
        /// <param name="apiKey">API Key</param>
        /// <returns></returns>
        public async Task <Users> AddUserAsync(string username, string password, string email, string apiKey)
        {
            if (apiKey != JwtOptions.ApiKey && IsInProduction)
            {
                throw new AuthenticationException("Invalid API Key.", AuthenticationExceptionType.InvalidApiKey);
            }

            if (!email.IsValidEmail())
            {
                throw new ArgumentException("Invalid email.");
            }

            var emailExists = await AuthDbContext.Users.AnyAsync(usr => usr.Email == email);

            if (emailExists)
            {
                throw new ArgumentException("A user with that Email ID already exists.");
            }

            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentException("Username must be provided.");
            }

            var usernameExists = await AuthDbContext.Users.AnyAsync(usr => usr.Username == username);

            if (usernameExists)
            {
                throw new ArgumentException("Username already exists.");
            }

            if (!password.IsValidPassword())
            {
                throw new AuthenticationException("Invalid password", AuthenticationExceptionType.InvalidPassword);
            }

            DateTime now = DateTime.Now;

            Users newUser = new Users()
            {
                // TODO: Let the database generate this for us
                UserId            = Guid.NewGuid(),
                CreatedOn         = now,
                PasswordExpiresOn = now.AddMonths(6),
                Email             = email,
                Username          = username,
                UsersRoles        = null,
                PasswordHash      = Crypto.HashPassword(password),
                PasswordCreatedOn = now
            };

            await AuthDbContext.AddAsync(newUser);

            await AuthDbContext.SaveChangesAsync();

            return(newUser);
        }
Esempio n. 6
0
        public virtual async Task <Guid> AddAsync(TEntity entity)
        {
            ArgumentGuard.NotNull(entity, nameof(entity));

            await _dbContext.AddAsync(entity);

            await _dbContext.SaveChangesAsync();

            return(entity.Id);
        }
        public async Task <IActionResult> Register(string userName, string password, string name, string homeAddress)
        {
            var user = new ApplicationUser
            {
                Name        = name,
                Access      = "Student",
                HomeAddress = homeAddress,
                UserName    = userName,
            };

            var result = await userManager.CreateAsync(user, password);

            if (result.Succeeded)
            {
                user = await userManager.FindByNameAsync(user.UserName);

                var prelim   = InitializeGrades("Prelim");
                var midterm  = InitializeGrades("Midterm");
                var preFinal = InitializeGrades("Prefinal");
                var final    = InitializeGrades("Final");
                await authDbContext.AddRangeAsync(prelim, midterm, preFinal, final);

                await authDbContext.SaveChangesAsync();

                var grade = new Grade
                {
                    AppUserID  = user.Id,
                    PrelimID   = prelim.ID,
                    MidtermID  = midterm.ID,
                    PrefinalID = preFinal.ID,
                    FinalID    = final.ID
                };

                await authDbContext.AddAsync(grade);

                await authDbContext.SaveChangesAsync();
            }

            return(RedirectToAction("Index", "Admin", new { area = "" }));
        }