コード例 #1
0
        public async Task <ActionResult> ChangeEmailAddress(ChangeEmailAddressViewModel model)
        {
            var userId = _userIdentity.GetUserId(this);
            var user   = _context.User.Where(u => u.Id == userId && u.Enabled && u.EmailVerified && u.Approved).FirstOrDefault();

            _appSensor.ValidateFormData(this, new List <string>()
            {
                "NewEmailAddress", "Password"
            });
            if (ModelState.IsValid)
            {
                var logonResult = await _userManager.TryLogOnAsync(_userIdentity.GetUserName(this), model.Password);

                if (logonResult.Success)
                {
                    if (user != null)
                    {
                        user.NewEmailAddressToken             = Guid.NewGuid().ToString().Replace("-", "");
                        user.NewEmailAddressRequestExpiryDate = DateTime.UtcNow.AddMinutes(15);
                        user.NewEmailAddress = model.NewEmailAddress;
                        // Send change username with link to recover password form
                        string emailBody    = EmailTemplates.ChangeEmailAddressPendingBodyText(user.FirstName, user.LastName, _configuration.ApplicationName, _configuration.WebsiteBaseUrl, user.NewEmailAddressToken);
                        string emailSubject = string.Format("{0} - Complete the change email address process", _configuration.ApplicationName);
                        _services.SendEmail(_configuration.DefaultFromEmailAddress, new List <string>()
                        {
                            user.UserName
                        }, null, null, emailSubject, emailBody, true);
                        user.UserLogs.Add(new UserLog()
                        {
                            Description = string.Format("Change email address request started to change from {0} to {1}", user.UserName, user.NewEmailAddress)
                        });
                        _context.SaveChanges();
                        return(View("ChangeEmailAddressPending"));
                    }
                }
                else
                {
                    Logger.Information("Failed Account ChangeEmailAddress Post, Password incorrect by requester {@requester}", _userIdentity.GetRequester(this, Core.Constants.AppSensorDetectionPointKind.AE1));
                    ModelState.AddModelError("Password", "The password is not correct");
                }
            }
            else
            {
                _appSensor.InspectModelStateErrors(this);
            }
            return(View(new ChangeEmailAddressViewModel(user.UserName, user.NewEmailAddress, user.NewEmailAddressRequestExpiryDate)));
        }
コード例 #2
0
 public void SignOut()
 {
     try
     {
         var userName = AuthenticationManager.User.Identity.Name;
         var user     = _context.User.Where(u => u.UserName == userName).FirstOrDefault();
         user.UserLogs.Add(new UserLog()
         {
             Description = "User Logged Off"
         });
         _context.SaveChanges();
     }
     catch {
     }
     finally
     {
         AuthenticationManager.SignOut();
     }
 }