public async Task <IActionResult> Registration(UserViewModel model) { User newUser = new User() { ID = new Guid(), Password = _encrypter.EncryptString(model.Password), Login = model.Login, Email = model.Email }; var role = _context.Role.FirstOrDefault(r => r.Name == "User"); await _unitOfWork.UserRepository.AddAsync(newUser); await _unitOfWork.UserRoleRepository.AddAsync(new UserRole() { ID = new Guid(), UserID = newUser.ID, RoleID = role.ID }); await _unitOfWork.SaveDBAsync(); Log.Logger.Information($"Info|{DateTime.Now}|New user {newUser.Login}|{newUser.ID}"); if (User != null) { await Authenticate(newUser); return(RedirectToAction("Index", "Home")); } return(View()); }
public async Task InitializeAsync() { if (!await _context.Role.AnyAsync(R => R.Name == "Admin")) { await _context.Role.AddAsync(new Role() { Name = "Admin", ID = new Guid() }); await _context.Role.ToListAsync(); await _unitOfWork.SaveDBAsync(); } if (!await _context.Role.AnyAsync(R => R.Name == "User")) { await _context.Role.AddAsync(new Role() { Name = "User", ID = new Guid() }); await _context.Role.ToListAsync(); await _unitOfWork.SaveDBAsync(); } if (!await _context.User.AnyAsync(U => U.Login == "CEO")) { await _context.User.AddAsync(new User() { Login = "******", ID = new Guid(), Password = _encrypter.EncryptString("qwerty"), Email = "*****@*****.**" }); await _context.User.ToListAsync(); await _unitOfWork.SaveDBAsync(); } if (!await _context.UserRoles.AnyAsync(UR => UR.UserID == _context.User .FirstOrDefault(U => U.Login == "CEO") .ID)) { await _context.UserRoles.AddAsync(new UserRole() { ID = new Guid(), UserID = _context.User.FirstOrDefault(U => U.Login == "CEO").ID, RoleID = _context.Role.FirstOrDefault(R => R.Name == "Admin").ID }); await _context.UserRoles.AddAsync(new UserRole() { ID = new Guid(), UserID = _context.User.FirstOrDefault(U => U.Login == "CEO").ID, RoleID = _context.Role.FirstOrDefault(R => R.Name == "User").ID }); await _unitOfWork.SaveDBAsync(); } }
/// <summary> /// Create admin if not created /// </summary> /// <returns></returns> public async Task InitializeAsync() { var adminInfo = new User(); _config.GetSection("AdminInfo").Bind(adminInfo); if (!await _context.Role.AnyAsync(R => R.Name == "Admin")) { await _unitOfWork.RoleRepository.AddAsync(new Role() { Name = "Admin", ID = new Guid() }); await _unitOfWork.SaveDBAsync(); } if (!await _context.Role.AnyAsync(R => R.Name == "User")) { await _unitOfWork.RoleRepository.AddAsync(new Role() { Name = "User", ID = new Guid() }); await _unitOfWork.SaveDBAsync(); } if (!await _context.User.AnyAsync(U => U.Login == adminInfo.Login)) { await _unitOfWork.UserRepository.AddAsync(new User() { Login = adminInfo.Login, ID = new Guid(), Password = _encrypter.EncryptString(adminInfo.Password), Email = adminInfo.Email }); await _unitOfWork.SaveDBAsync(); } if (!await _context.UserRoles.AnyAsync(UR => UR.UserID == _context.User .FirstOrDefault(U => U.Login == adminInfo.Login) .ID)) { await _context.UserRoles.AddAsync(new UserRole() { ID = new Guid(), UserID = _context.User.FirstOrDefault(U => U.Login == adminInfo.Login).ID, RoleID = _context.Role.FirstOrDefault(R => R.Name == "Admin").ID }); await _context.UserRoles.AddAsync(new UserRole() { ID = new Guid(), UserID = _context.User.FirstOrDefault(U => U.Login == adminInfo.Login).ID, RoleID = _context.Role.FirstOrDefault(R => R.Name == "User").ID }); await _unitOfWork.SaveDBAsync(); } }