Exemple #1
0
        public async Task <IActionResult> ChangeAvatar(IndexViewModel model)
        {
            var user = await _userManager.GetUserAsync(HttpContext.User);

            if (model.ProfilePicture == "gravatar")
            {
                user.ProfilePicture = Gravatar.GetLink(user.Email);
            }
            else
            {
                user.ProfilePicture = model.ProfilePicture;
            }

            _context.Users.Update(user);
            await _context.SaveChangesAsync();

            var NewModel = new IndexViewModel
            {
                HasPassword       = await _userManager.HasPasswordAsync(user),
                PhoneNumber       = await _userManager.GetPhoneNumberAsync(user),
                TwoFactor         = await _userManager.GetTwoFactorEnabledAsync(user),
                Logins            = await _userManager.GetLoginsAsync(user),
                BrowserRemembered = await _signInManager.IsTwoFactorClientRememberedAsync(user),
                ProfilePicture    = user.ProfilePicture
            };

            return(View("Index", NewModel));
        }
        public async Task <IActionResult> Register(LogRegModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                if (!_userManager.Users.Any(x => x.ProfileName == model.Register.ProfileName))
                {
                    var user = new ApplicationUser {
                        UserName = model.Register.Email, Email = model.Register.Email
                    };

                    // ApplicationUser does not take ProfileName in a construtor.
                    user.ProfileName    = model.Register.ProfileName;
                    user.ProfilePicture = Gravatar.GetLink(model.Register.Email);

                    // This calls our overridden function in ProfileNameClaimsPrincipalFactory.cs
                    var result = await _userManager.CreateAsync(user, model.Register.Password);

                    if (result.Succeeded)
                    {
                        // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=532713
                        // Send an email with this link
                        //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                        //var callbackUrl = Url.Action(nameof(ConfirmEmail), "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                        //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                        //    $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");

                        await _signInManager.SignInAsync(user, isPersistent : false);

                        _logger.LogInformation(3, "User created a new account with password.");
                        return(RedirectToLocal(returnUrl));
                    }
                    AddErrors(result);
                }
                AddErrors(IdentityResult.Failed(new IdentityError {
                    Description = "Profile Name already taken"
                }));
            }

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