Beispiel #1
0
        private async Task <ProfileVM> GetProfileVM(IUser user, ChangeProfileModel model = null, string errorMessage = null, string successMessage = null)
        {
            var externalSchemes = await signInManager.GetExternalAuthenticationSchemesAsync();

            var externalProviders = externalSchemes.Select(x => new ExternalProvider(x.Name, x.DisplayName)).ToList();

            var result = new ProfileVM
            {
                Id                = user.Id,
                Email             = user.Email,
                ErrorMessage      = errorMessage,
                ExternalLogins    = user.Logins,
                ExternalProviders = externalProviders,
                DisplayName       = user.DisplayName(),
                HasPassword       = await userManager.HasPasswordAsync(user),
                HasPasswordAuth   = identityOptions.Value.AllowPasswordAuth,
                SuccessMessage    = successMessage
            };

            if (model != null)
            {
                SimpleMapper.Map(model, result);
            }

            return(result);
        }
Beispiel #2
0
        public Task <IActionResult> Profile(ChangeProfileModel model)
        {
            return(MakeChangeAsync(async user =>
            {
                user.UpdateEmail(model.Email);
                user.SetDisplayName(model.DisplayName);

                return await userManager.UpdateAsync(user);
            }, "Account updated successfully. Please logout and login again to see the changes."));
        }
Beispiel #3
0
        private async Task <ProfileVM> GetProfileVM(IUser user, ChangeProfileModel model = null)
        {
            var result = new ProfileVM
            {
                Email           = user.Email,
                DisplayName     = user.DisplayName(),
                PictureUrl      = user.PictureUrl(),
                HasPassword     = await userManager.HasPasswordAsync(user),
                HasPasswordAuth = identityOptions.Value.AllowPasswordAuth
            };

            if (model != null)
            {
                SimpleMapper.Map(model, result);
            }

            return(result);
        }
Beispiel #4
0
 public Task <IActionResult> UpdateProfile(ChangeProfileModel model)
 {
     return(MakeChangeAsync(user => userManager.UpdateAsync(user, model.Email, model.DisplayName),
                            "Account updated successfully."));
 }
Beispiel #5
0
        private async Task <IActionResult> MakeChangeAsync(Func <IUser, Task <IdentityResult> > action, string successMessage, ChangeProfileModel model = null)
        {
            var user = await userManager.GetUserAsync(User);

            if (!ModelState.IsValid)
            {
                return(View(nameof(Profile), await GetProfileVM(user, model)));
            }

            string errorMessage;

            try
            {
                var result = await action(user);

                if (result.Succeeded)
                {
                    await signInManager.SignInAsync(user, true);

                    return(RedirectToAction(nameof(Profile), new { successMessage }));
                }

                errorMessage = string.Join(". ", result.Errors.Select(x => x.Description));
            }
            catch
            {
                errorMessage = "An unexpected exception occurred.";
            }

            return(View(nameof(Profile), await GetProfileVM(user, model, errorMessage)));
        }