protected void MakeAdminButton_Click(object sender, EventArgs e) { string userId = this.Request.QueryString["UserId"]; var userStore = new UserStore<User>(new TheCodeBookProjectDbContext()); var userManager = new UserManager<User>(userStore); userManager.AddToRole(userId, "admin"); this.Response.Redirect("~/Admin/Users.aspx"); }
public void SeedUsers(AuctionDbContext context) { var store = new UserStore<User>(context); var manager = new UserManager<User>(store); User admin = new User { UserName = "******", Email = "*****@*****.**", FirstName = "Tosho", LastName = "Ivanov", PhoneNumber = "+3591234569", SecurityStamp = Guid.NewGuid().ToString(), PasswordHash = this.hasher.HashPassword("1"), }; User pesho = new User { UserName = "******", Email = "*****@*****.**", FirstName = "Pesho", LastName = "Goshov", PhoneNumber = "+3591234567", SecurityStamp = Guid.NewGuid().ToString(), PasswordHash = this.hasher.HashPassword("1"), }; User gosho = new User { UserName = "******", Email = "*****@*****.**", FirstName = "Gosho", LastName = "Toshov", PhoneNumber = "+3591234568", SecurityStamp = Guid.NewGuid().ToString(), PasswordHash = this.hasher.HashPassword("1"), }; this.Users.Add(pesho); this.Users.Add(gosho); this.Users.Add(admin); context.Users.AddOrUpdate(this.Users.ToArray()); context.SaveChanges(); var adminId = context.Users.FirstOrDefault(u => u.UserName == "*****@*****.**").Id; manager.AddToRole(adminId, "Admin"); context.SaveChanges(); }
public ActionResult AppUsers_Create([DataSourceRequest]DataSourceRequest request, AppUserGridViewModel appUser) { var userManager = new UserManager<AppUser>(new UserStore<AppUser>(this.context)); var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(this.context)); if (!roleManager.Roles.Any()) { roleManager.Create(new IdentityRole { Name = "User" }); roleManager.Create(new IdentityRole { Name = "Admin" }); } var newId = string.Empty; if (this.ModelState.IsValid) { AppUser userToCreate = new AppUser { Avatar = appUser.Avatar, Email = appUser.Email, UserName = appUser.UserName, FirstName = appUser.FirstName, LastName = appUser.LastName }; userManager.Create(userToCreate, appUser.Password); userManager.AddToRole(userToCreate.Id, "User"); newId = userToCreate.Id; } var userToDisplay = this.users.All() .To<AppUserGridViewModel>() .FirstOrDefault(x => x.Id == newId); return this.Json(new[] { userToDisplay }.ToDataSourceResult(request, this.ModelState)); }
public ActionResult ResetPin(string card_id) { var UserManager = new UserManager<ApplicationUser> ( new UserStore<ApplicationUser>("CoreDB_Server") ); string newPassword = DateTime.Now.ToString("ssHHmm"); if (!String.IsNullOrEmpty(Helper.DEFAULT_PIN)) newPassword = Helper.DEFAULT_PIN; dynamic current_user = Helper.DataHelper.Get("users", Query.EQ("UserName", card_id)); Helper.DataHelper.Delete("users", current_user._id); var new_user = new ApplicationUser() { UserName = card_id, }; var result = UserManager.Create(new_user, newPassword); if (result.Succeeded) { //add Roles to User string[] roles = new string[] { "CUSTOMER" }; var roleResult = UserManager.AddToRole(new_user.Id, roles[0]); dynamic current_profile = Helper.DataHelper.Get("profile", Query.EQ("user_name", card_id)); //CMSDBDataContext db = new CMSDBDataContext(); //var card = (from element in db.Cards where element.CardId.Equals(card_id) select element).SingleOrDefault(); //if (card != null) //{ // card.IsActived = false; // db.SubmitChanges(); // db.Dispose(); //} if (current_profile != null) { current_profile.Pin = 1; Helper.DataHelper.Save("profile", current_profile); } } //string token = UserManager.GenerateUserToken("ResetPassword", card_id); //bool succeded = UserManager.ResetPassword(card_id, token, newPassword).Succeeded; ViewBag.Result = "thành công. Mã PIN mới là " + newPassword; return View(); }