public async Task <ActionResult> EditUser(EditUserViewModel model) { TheCodingVineDbContext db = new TheCodingVineDbContext(); UserManager <AppUser> userMgr = new UserManager <AppUser>(new UserStore <AppUser>(db)); RoleManager <IdentityRole> roleMgr = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db)); var theUser = userMgr.FindById(model.User.Id); List <string> roleNames = new List <string>(); foreach (var role in theUser.Roles) { var roleName = roleMgr.FindById(role.RoleId).Name; roleNames.Add(roleName); } foreach (var name in roleNames) { IdentityResult deletionResult = userMgr.RemoveFromRole(model.User.Id, name); } var updatedRoleName = roleMgr.FindById(model.SelectedRoleId).Name; var updatedPassword = model.NewPassword; var updateduserName = model.User.Email; userMgr.AddToRole(theUser.Id, updatedRoleName); userMgr.AddPassword(theUser.Id, updatedPassword); userMgr.SetEmail(theUser.Id, updateduserName); theUser.UserName = updateduserName; userMgr.Update(theUser); return(RedirectToAction("MyBloggers", "Blog")); }
// For more information on configuring authentication, please visit https://go.microsoft.com/fwlink/?LinkId=301864 public void ConfigureAuth(IAppBuilder app) { // Configure the db context, user manager and signin manager to use a single instance per request var DbContext = new TheCodingVineDbContext(); app.CreatePerOwinContext(DbContext.Create); app.CreatePerOwinContext <ApplicationUserManager>(ApplicationUserManager.Create); app.CreatePerOwinContext <ApplicationSignInManager>(ApplicationSignInManager.Create); }
public ActionResult AddBlogPost(BlogVM model) { if (ModelState.IsValid) { if (string.IsNullOrEmpty(model.Blog.Title)) { ModelState.AddModelError("Title", "You must enter a title."); } else if (string.IsNullOrEmpty(model.Blog.Content)) { ModelState.AddModelError("Content", "You must enter something in the blog body."); } else { TheCodingVineDbContext db = new TheCodingVineDbContext(); BlogManager manager = BlogManagerFactory.Create(); if (model.Blog.TagInputs != null) { string[] tags = model.Blog.TagInputs.Split(','); foreach (var tag in tags) { var searchTag = new SearchTag() { SearchTagBody = tag }; model.Blog.SearchTags.Add(searchTag); } model.Blog.TagInputs = null; } UserManager <AppUser> userManager = new UserManager <AppUser>(new UserStore <AppUser>(db)); model.Blog.BlogWriter = userManager.FindByName(User.Identity.Name); manager.AddBlog(model.Blog); return(RedirectToAction("MyBlogs")); } } return(View(model)); }
public static BlogManager Create() { string mode = ConfigurationManager.AppSettings["Mode"].ToString(); switch (mode) { case "InMemoryRepo": return(new BlogManager(new InMemoryRepo())); case "DbContext": TheCodingVineDbContext entityRepo = new TheCodingVineDbContext(); return(new BlogManager(entityRepo)); default: throw new Exception("Mode value in app config is not valid"); } }
public async Task <ActionResult> EditUser(string id) { TheCodingVineDbContext db = new TheCodingVineDbContext(); UserManager <AppUser> userMgr = new UserManager <AppUser>(new UserStore <AppUser>(db)); RoleManager <IdentityRole> roleMgr = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db)); var vm = new EditUserViewModel(); var user = db.GetBlogger(id); vm.Roles = RoleManager.Roles.Select(r => new SelectListItem { Text = r.Name, Value = r.Id }).ToList(); vm.SelectedRoleId = user.Roles.ElementAt(0).RoleId; vm.SetUser(user); return(View(vm)); }