public void AlocateRoleToUser(string username, int roleId) { UsersRepository ur = new UsersRepository(); User u = ur.GetUser(username); Role r = ur.GetRole(roleId); if (ur.IsUserAllocatedToRole(u, r)) { throw new Exception("User cannot be allocated to role because he already has that role"); } else { ur.AllocateRoleToUser(u, r); } }
public ActionResult Registration(User u, string ConfirmPassword) { try { if (ModelState.IsValid) { if (ConfirmPassword == u.Password) { UsersRepository ur = new UsersRepository(); if (ur.DoesUsernameExist(u.Username) == true) { ViewBag.Error = "Username already exist"; //return View(u); } else { u.Password = Encryption.HashPassword(u.Password) ; ur.AddUser(u); Role defaultrole = ur.GetDefaultRole(); ur.AllocateRoleToUser(u, defaultrole); ViewBag.Message = "Registration successful"; ModelState.Clear(); } } else { ViewBag.Error = "password do not match"; } } else { ViewBag.Error = "Check your inputs"; } } catch (Exception ex) { ViewBag.Error = "Registration failed!"; } return(View()); }
public void Register(User u) { UsersRepository ur = new UsersRepository(); if (ur.DoesUsernameExist(u.Username) == true) { throw new UsernameExistsException("Username already exists"); } else { u.Password = new Encryption().HashString(u.Password); AsymmetricParameters myParameters = new Encryption().GenerateAsymmetricParameters(); u.PublicKey = myParameters.PublicKey; u.PrivateKey = myParameters.PrivateKey; ur.AddUser(u); ur.AllocateRoleToUser(u, ur.GetRole(1)); } }