示例#1
0
        public async Task <IActionResult> ChangeMail(ChangeMailViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var uid = User.GetUserId();

            if (string.IsNullOrEmpty(model.NewEmail))
            {
                ModelState.AddModelError(string.Empty, "新邮箱不能为空");
                return(View());
            }
            if (model.NewEmail == model.OldMail)
            {
                ModelState.AddModelError(string.Empty, "新邮箱不能和旧邮箱重复");
                return(View());
            }
            using (var ldap = CreateLdap())
            {
                var entries = ldap.Search(settings.Options.LdapPeopleOU, LdapScope.SUB, string.Format("mail={0}", model.NewEmail));
                var entry   = entries.FirstOrDefault();
                if (entry != null && entry.DN != string.Format("uid={0},{1}", uid, settings.Options.LdapPeopleOU))
                {
                    ModelState.AddModelError(string.Empty, "邮箱地址已存在");
                    return(View());
                }
            }
            ChangeMail(uid, model.NewEmail);
            return(RedirectToAction(nameof(Index), new { Message = "邮箱修改成功" }));
        }
示例#2
0
        public async Task <ActionResult> ChangeMail([Bind(Include = "Email")] ChangeMailViewModel model)
        {
            int            id  = GetUserId();
            SignInLogModel log = new SignInLogModel();

            log.ClientIp    = GetClientIp();
            log.UserModelId = id;
            log.UserCode    = GetSigninId();

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            IdentityResult result = await UserManager.SetEmailAsync(id, model.Email);

            if (!result.Succeeded)
            {
                AddErrors(result);
                return(View(model));
            }

            log.SetStatus(4);
            dbContext.SignInLogModels.Add(log);
            dbContext.SaveChanges();
            return(View("MailChanged"));
        }
示例#3
0
 public async Task<IActionResult> ChangeMail(ChangeMailViewModel model)
 {
     if (!ModelState.IsValid)
     {
         return View(model);
     }
     var uid = User.GetUserId();
     if(string.IsNullOrEmpty(model.NewEmail))
     {
         ModelState.AddModelError(string.Empty, "新邮箱不能为空");
         return View();
     }
     if(model.NewEmail == model.OldMail)
     {
         ModelState.AddModelError(string.Empty, "新邮箱不能和旧邮箱重复");
         return View();
     }
     using (var ldap = CreateLdap())
     {
         var entries = ldap.Search(settings.Options.LdapPeopleOU, LdapScope.SUB, string.Format("mail={0}", model.NewEmail));
         var entry = entries.FirstOrDefault();
         if (entry != null && entry.DN != string.Format("uid={0},{1}", uid, settings.Options.LdapPeopleOU))
         {
             ModelState.AddModelError(string.Empty, "邮箱地址已存在");
             return View();
         }
     }
     ChangeMail(uid, model.NewEmail);
     return RedirectToAction(nameof(Index), new { Message = "邮箱修改成功" });
 }
        public async Task <ActionResult> ChangeEmail(ChangeMailViewModel changeMailViewModel)
        {
            if (changeMailViewModel.UserId != User.NameIdentifier())
            {
                return(BadRequest());
            }

            var mailExist = await _userManager.FindByEmailAsync(changeMailViewModel.NewMail);

            if (mailExist != null)
            {
                return(Conflict());
            }

            var user = await _userManager.FindByIdAsync(changeMailViewModel.UserId);

            if (mailExist != null || user == null || changeMailViewModel.NewMail == user.Email || user.EmailConfirmed == false)
            {
                return(NotFound());
            }

            try
            {
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));

                var callbackUrl = Url.Page(
                    "/Account/ConfirmEmail",
                    pageHandler: null,
                    values: new { area = "Identity", userId = user.Id, code = code, returnUrl = Url.Content("~/") },
                    protocol: Request.Scheme);

                var frontWebSite   = UriHelper.BuildAbsolute(Request.Scheme, Request.Host);
                var logoUrl        = UriHelper.BuildAbsolute(Request.Scheme, Request.Host, "/assets/img/logo_full.png");
                var unSubscribeUrl = UriHelper.BuildAbsolute(Request.Scheme, Request.Host, "/Account/UnSubscribe");

                await SendEmailConfirmation(changeMailViewModel.NewMail, EmailTemplateEnum.EmailConfirmation, new Dictionary <string, string>()
                {
                    { "frontWebSite", frontWebSite },
                    { "logoUrl", logoUrl },
                    { "unSubscribeUrl", unSubscribeUrl },
                    { "callbackUrl", callbackUrl }
                });

                user.EmailConfirmed     = false;
                user.UserName           = user.Email = changeMailViewModel.NewMail;
                user.NormalizedUserName = user.NormalizedEmail = changeMailViewModel.NewMail.ToUpper();
                await _userManager.UpdateAsync(user);

                return(Ok());
            }
            catch (Exception ex)
            {
                throw;
            }
        }
示例#5
0
 public IActionResult ChangeMail()
 {
     using (var ldap = CreateLdap())
     {
         var entry = ldap.SearchOne(string.Format("uid={0},{1}", User.GetUserId(), settings.Options.LdapPeopleOU), LdapScope.BASE, string.Empty);
         if (entry == null)
         {
             ModelState.AddModelError(string.Empty, "系统发生异常或者您的用户数据已被删除,请联系管理人员");
             return(View());
         }
         var oldMail = entry.getAttribute("mail").StringValue;
         var model   = new ChangeMailViewModel
         {
             OldMail = oldMail
         };
         return(View(model));
     }
 }
示例#6
0
 public IActionResult ChangeMail()
 {
     using (var ldap = CreateLdap())
     {
         var entry = ldap.SearchOne(string.Format("uid={0},{1}", User.GetUserId(), settings.Options.LdapPeopleOU), LdapScope.BASE, string.Empty);
         if (entry == null)
         {
             ModelState.AddModelError(string.Empty, "系统发生异常或者您的用户数据已被删除,请联系管理人员");
             return View();
         }
         var oldMail = entry.getAttribute("mail").StringValue;
         var model = new ChangeMailViewModel
         {
             OldMail = oldMail
         };
         return View(model);
     }
 }
        public async Task <ActionResult> ChangeMail(ChangeMailViewModel model)
        {
            var userId = User.Identity.GetUserId();
            var user   = await UserManager.FindByIdAsync(userId);

            if (user != null)
            {
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }
                if (UserManager.CheckPassword(user, model.Password))
                {
                    user.Email    = model.Email;
                    user.UserName = model.Email;
                    await UserManager.UpdateAsync(user);

                    return(RedirectToAction("Index", new { Message = "Mail changed!" }));
                }
            }
            //AddErrors(result);
            return(View(model));
        }
 //
 // GET: /Manage/ChangeMail
 public ActionResult ChangeMail()
 {
     string mail = UserManager.FindById(User.Identity.GetUserId()).Email;
     ChangeMailViewModel model = new ChangeMailViewModel { OldMail = mail};
     return View(model);
 }