public async Task <PartialViewResult> EPChangePassword(EditProfileViewModel model) { ApplicationDbContext db = new ApplicationDbContext(); int WebUserID = int.Parse(Request.Cookies["WebUserID"].Value); Web_user WebUser = db.Web_Users.Find(WebUserID); ApplicationUser IdenUser = await UserManager.FindByIdAsync(WebUser.UserID); if (UserManager.CheckPassword(IdenUser, model.CurrentPassword)) { IdenUser.PasswordHash = UserManager.PasswordHasher.HashPassword(model.NewPassword); var result = await UserManager.UpdateAsync(IdenUser); if (result.Succeeded) { return(PartialView("_ChangePasswordPartial")); } } return(PartialView("_ChangePassFailed", model)); }
public PartialViewResult EditProfile(EditProfileViewModel model) { ApplicationDbContext db = new ApplicationDbContext(); var allowedExtensions = new[] { ".Jpg", ".png", ".jpg", "jpeg", ".JPG", ".PNG", ".JPEG" }; int webUserID = int.Parse(Request.Cookies["WebUserID"].Value); Web_user webUser = db.Web_Users.Find(webUserID); if (model.AvatarFile != null) { var filename = Path.GetFileName(model.AvatarFile.FileName); var extension = Path.GetExtension(model.AvatarFile.FileName); if (allowedExtensions.Contains(extension)) { if (System.IO.File.Exists(webUser.UserImage)) { System.IO.File.Delete(webUser.UserImage); } string name = Path.GetFileNameWithoutExtension(filename); string myImage = name + "_" + model.UserName + extension; var savePath = Path.Combine(Server.MapPath("~/Content/ProfileImage"), myImage); var imagePath = Path.Combine("/Content/ProfileImage/", myImage); webUser.UserImage = imagePath; model.AvatarFile.SaveAs(savePath); webUser.Name = model.UserName; db.SaveChanges(); return(PartialView("_EditProfilePartial")); } return(PartialView("_WrongFileType")); } else { webUser.Name = model.UserName; db.SaveChanges(); return(PartialView("_EditProfilePartial")); } // action when user didnt have a profile image }
public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { var u = new Web_user() { Name = info.ExternalIdentity.Name, Birthday = new DateTime(1970, 1, 1), UserID = user.Id, UserImage = @"/Content/Images/default.jpg" }; if (model.AsTeacher) { using (var db = new ApplicationDbContext()) { var t = new Teacher() { User = u }; db.Web_Users.Add(u); db.Teachers.Add(t); db.SaveChanges(); } UserManager.AddToRole(user.Id, "Teacher"); } else { using (var db = new ApplicationDbContext()) { var s = new Student() { web_User = u }; db.Web_Users.Add(u); db.Students.Add(s); db.SaveChanges(); } UserManager.AddToRole(user.Id, "Student"); } result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(await SignInAsync(user, returnUrl)); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public async Task <ActionResult> Register(RegisterViewModel model) { var temp = new RegisterViewModel() { Email = model.Email, Password = model.Password, ConfirmPassword = model.ConfirmPassword }; if (true) { var user = new ApplicationUser { UserName = temp.Email, Email = temp.Email }; var result = await UserManager.CreateAsync(user, temp.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); await UserManager.SendEmailAsync(user.Id, "Welcome to ABC english", "This is a confirmation of the email you register to our website"); var u = new Web_user() { Name = model.Name, Birthday = model.Birthday, UserID = user.Id, UserImage = @"\Content\Images\default.jpg" }; if (model.AsTeacher) { using (var db = new ApplicationDbContext()) { var t = new Teacher() { User = u }; db.Web_Users.Add(u); db.Teachers.Add(t); db.SaveChanges(); } UserManager.AddToRole(user.Id, "Teacher"); } else { using (var db = new ApplicationDbContext()) { var s = new Student() { web_User = u }; db.Web_Users.Add(u); db.Students.Add(s); db.SaveChanges(); } UserManager.AddToRole(user.Id, "Student"); } return(await SignInAsync(user, "~/Home/Index")); } AddErrors(result); } return(RedirectToAction("About", "Home")); }