public ActionResult AddRole(AspNetRole role) { lamodeEntities context = new lamodeEntities(); context.AspNetRoles.Add(role); context.SaveChanges(); return(View()); }
public ActionResult AddUserToRole() { lamodeEntities context = new lamodeEntities(); AspNetRole aspNetRole = new AspNetRole(); AspNetUser aspNetUser = new AspNetUser(); var list = context.AspNetRoles.ToList(); var listUser = context.AspNetUsers.ToList(); ViewBag.RoleList = list.ToList(); ViewBag.UserList = listUser.ToList(); return(View()); }
public ActionResult AddUserToRole(string userName, string Id) { lamodeEntities context = new lamodeEntities(); AspNetUser user = context.AspNetUsers .Where(u => u.UserName == userName).FirstOrDefault(); AspNetRole role = context.AspNetRoles .Where(r => r.Id == Id).FirstOrDefault(); user.AspNetRoles.Add(role); context.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Login(Login login) { // UserStore and UserManager manages data retreival. UserStore <IdentityUser> userStore = new UserStore <IdentityUser>(); UserManager <IdentityUser> manager = new UserManager <IdentityUser>(userStore); IdentityUser identityUser = manager.Find(login.UserName, login.Password); if (ModelState.IsValid) { if (identityUser != null) { IAuthenticationManager authenticationManager = HttpContext.GetOwinContext().Authentication; authenticationManager .SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, login.UserName), }, DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.Name, ClaimTypes.Role); // SignIn() accepts ClaimsIdentity and issues logged in cookie. authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity); lamodeEntities context = new lamodeEntities(); var user = context.AspNetUsers.Where(u => u.UserName == login.UserName).FirstOrDefault(); var role = manager.GetRoles(user.Id); if (role[0] == "Admin") { return(RedirectToAction("AdminOnly", "Home")); } else if (role[0] == "VIPUser") { return(RedirectToAction("VIPUser", "Home")); } else if (role[0] == "SpecialUser") { return(RedirectToAction("SpecialUser", "Home")); } } return(RedirectToAction("SecureArea", "Home")); } return(View()); }
public ActionResult Register(RegisteredUser newUser, string registeredPeople) { ViewBag.registeredPeople = registeredPeople; var userStore = new UserStore <IdentityUser>(); var manager = new UserManager <IdentityUser>(userStore); var identityUser = new IdentityUser() { UserName = newUser.UserName, Email = newUser.Email }; IdentityResult result = manager.Create(identityUser, newUser.Password); lamodeEntities db = new lamodeEntities(); if (result.Succeeded) { var authenticationManager = HttpContext.Request.GetOwinContext().Authentication; var userIdentity = manager.CreateIdentity(identityUser, DefaultAuthenticationTypes.ApplicationCookie); authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity); } var user = manager.Users.FirstOrDefault(u => u.UserName == newUser.UserName); //for the rest of data from AspNetUser table AdditionalUserInfo additionalUserInfo = new AdditionalUserInfo(); additionalUserInfo.Id = user.Id; additionalUserInfo.CompanyName = newUser.CompanyName; additionalUserInfo.DateOfBirth = newUser.DateOfBirth; //this gets current country from user //additionalUserInfo.Nationality = newUser.Nationality; //string country = RegionInfo.CurrentRegion.DisplayName; //ViewBag.country = country; additionalUserInfo.Country = newUser.country; additionalUserInfo.City = newUser.City; additionalUserInfo.Province = newUser.state; additionalUserInfo.TellUsMore = newUser.TellUsMore; additionalUserInfo.Website = newUser.Website; additionalUserInfo.ZipCode = newUser.ZipCode; additionalUserInfo.Gender = newUser.Gender; try { db.AdditionalUserInfoes.Add(additionalUserInfo); db.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { System.Console.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } return(RedirectToAction("MoreRegisterationForIndividuals", newUser)); }