Exemplo n.º 1
0
        public async Task <ActionResult> ResetPassword(ResetPasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var user = await UserManager.FindByNameAsync(model.Email);

            if (user == null)
            {
                // Don't reveal that the user does not exist
                return(RedirectToAction("ResetPasswordConfirmation", "Account"));
            }
            var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);

            if (result.Succeeded)
            {
                #region Set email to confirmed here
                try
                {
                    if (!user.EmailConfirmed)
                    {
                        using (var dbContext = new CodeLib.Models.Entities())
                        {
                            int userId = dbContext.AspNetUser_Update(user.Id, true).FirstOrDefault() ?? -1;

                            if (userId != user.Id)
                            {
                                CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, "The DB failed to update AspNetUser to EmailConfirmed.", "AccountController.cs >> ResetPassword() >> AspNetUser_Update()", user.Id.ToString());
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, SiteUtils.GetPageName(), null, ex.Message, ex.StackTrace, user.Email);
                }
                #endregion

                return(RedirectToAction("ResetPasswordConfirmation", "Account"));
            }
            AddErrors(result);
            return(View());
        }
        public ActionResult UpdateProfile(UpdateProfileViewModel profileModel)
        {
            if (ModelState.IsValid)
            {
                if (profileModel.UserId > 0)
                {
                    #region Update AppUser Record
                    try
                    {
                        int userId         = 0;
                        int?loggedInUserId = (Request.IsAuthenticated) ? int.Parse(User.Identity.GetUserId()) : new int();

                        using (var dbContext = new CodeLib.Models.Entities())
                        {
                            userId = dbContext.AppUser_InsertUpdate(profileModel.UserId, null,
                                                                    null, profileModel.FirstName, profileModel.LastName, profileModel.Email, loggedInUserId).FirstOrDefault() ?? -1;
                        }

                        if (userId == profileModel.UserId)
                        {
                            return(RedirectToAction("Index", new { Message = ManageMessageId.UpdateProfileSuccess }));
                        }
                        else
                        {
                            AddErrors(new IdentityResult(new string[] { "Oops! An error has occurred. " + CommonObjects.ERROR_MSG_SUPPORT }));
                            CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, "The DB failed to save AppUser record for " + profileModel.FirstName + " " + profileModel.LastName, "ManageController.cs >> UpdateProfile() >> AppUser_InsertUpdate()", profileModel.UserId.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        AddErrors(new IdentityResult(new string[] { "Oops! An error has occurred. " + CommonObjects.ERROR_MSG_SUPPORT }));
                        CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, SiteUtils.GetPageName(), null, ex.Message, ex.StackTrace, profileModel.UserId.ToString());
                    }
                    #endregion
                }
            }

            return(View(profileModel));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> UserMgmt(UserViewModel userModel)
        {
            if (ModelState.IsValid || (userModel.UserId > 0 && string.IsNullOrWhiteSpace(userModel.Password)))
            {
                ApplicationUser identityUser     = null;
                IdentityResult  result           = null;
                int?            userRoleId       = null;
                bool            sendWelcomeEmail = (userModel.UserId <= 0);

                if (userModel.UserId <= 0)
                {
                    // Create the AspNet Identity user
                    identityUser = new CodeLib.ApplicationUser {
                        UserName = userModel.Email, Email = userModel.Email
                    };
                    result = await UserManager.CreateAsync(identityUser, userModel.Password);

                    if (result.Succeeded)
                    {
                        userModel.UserId = identityUser.Id;
                        userRoleId       = (int)RoleIdEnum.AppUser;
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }

                if (result == null || result.Succeeded)
                {
                    #region Create AppUser Record
                    try
                    {
                        AppUser appUser        = null;
                        int?    loggedInUserId = (Request.IsAuthenticated) ? User.Identity.GetUserId <int>() : new int();

                        using (var dbContext = new CodeLib.Models.Entities())
                        {
                            int userId = dbContext.AppUser_InsertUpdate(userModel.UserId, userModel.StatusId,
                                                                        userRoleId, userModel.FirstName, userModel.LastName, userModel.Email, loggedInUserId).FirstOrDefault() ?? -1;

                            if (userId == userModel.UserId)
                            {
                                appUser = new AppUser
                                {
                                    UserId       = userId,
                                    StatusId     = userModel.StatusId,
                                    FirstName    = userModel.FirstName,
                                    LastName     = userModel.LastName,
                                    Email        = userModel.Email,
                                    IdentityUser = identityUser
                                };
                            }
                            else
                            {
                                CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, "The DB failed to save AppUser record for " + userModel.FirstName + " " + userModel.LastName, "AdminController.cs >> UserMgmt() >> AppUser_InsertUpdate()", identityUser.Id.ToString());
                            }
                        }

                        if (appUser != null)
                        {
                            if (sendWelcomeEmail)
                            {
                                // Send an email with this link
                                string code = await UserManager.GenerateEmailConfirmationTokenAsync(appUser.UserId);

                                var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = appUser.UserId, code = code }, protocol: Request.Url.Scheme);

                                if (!CodeLib.Email.EmailTemplate.SendRegistrationEmail(appUser, callbackUrl))
                                {
                                    CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, "The registration confirmation email failed to send to " + appUser.Email, "AdminController.cs >> UserMgmt()", appUser.UserId.ToString());
                                }
                            }

                            return(RedirectToAction("UsersMgmt"));
                        }
                        else
                        {
                            AddErrors(new IdentityResult(new string[] { "Oops! An error has occurred. " + CommonObjects.ERROR_MSG_SUPPORT }));
                        }
                    }
                    catch (Exception ex)
                    {
                        AddErrors(new IdentityResult(new string[] { "Oops! An error has occurred. " + CommonObjects.ERROR_MSG_SUPPORT }));
                        CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, SiteUtils.GetPageName(), null, ex.Message, ex.StackTrace, userModel.Email);
                    }
                    #endregion
                }
            }

            var statusList = await CommonDAL.GetLookupList(LookupTypeIdEnum.AppUserStatus);

            userModel.StatusList = statusList.Select(status => new SelectListItem {
                Text = status.Descr, Value = status.LookupId.ToString()
            }).ToList();

            return(View(userModel));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Register(UserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new CodeLib.ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    #region Create AppUser Record
                    try
                    {
                        AppUser appUser = null;

                        using (var dbContext = new CodeLib.Models.Entities())
                        {
                            int userId = dbContext.AppUser_InsertUpdate(user.Id, (int)DatabaseIdEnum.UserStatus_Active,
                                                                        (int)RoleIdEnum.AppUser, model.FirstName, model.LastName, null, user.Id).FirstOrDefault() ?? -1;

                            if (userId == user.Id)
                            {
                                appUser = new AppUser
                                {
                                    UserId       = user.Id,
                                    FirstName    = model.FirstName,
                                    LastName     = model.LastName,
                                    Email        = model.Email,
                                    IdentityUser = user
                                };
                            }
                            else
                            {
                                CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, "The DB failed to create AppUser record for " + model.FirstName + " " + model.LastName, "AccountController.cs >> Register() >> AppUser_InsertUpdate()", user.Id.ToString());
                            }
                        }

                        if (appUser != null && appUser.IdentityUser != null)
                        {
                            // For more information on how to enable account confirmation and password reset please visit http://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>");

                            if (!EmailTemplate.SendRegistrationEmail(appUser, callbackUrl))
                            {
                                CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, "The registration confirmation email failed to send to " + user.Email, "AccountController.cs >> Register()", user.Id.ToString());
                            }

                            return(RedirectToAction("Login", "Account"));
                        }
                        else
                        {
                            AddErrors(new IdentityResult(new string[] { "Oops! An error has occurred. " + CommonObjects.ERROR_MSG_SUPPORT }));
                        }
                    }
                    catch (Exception ex)
                    {
                        AddErrors(new IdentityResult(new string[] { "Oops! An error has occurred. " + CommonObjects.ERROR_MSG_SUPPORT }));
                        CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, SiteUtils.GetPageName(), null, ex.Message, ex.StackTrace, user.Email);
                    }
                    #endregion
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }