Пример #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            var updateduser = (ApplicationUser)user;

            updateduser.Name        = Input.Name;
            updateduser.Country     = Input.Country;
            updateduser.State       = Input.State;
            updateduser.Address1    = Input.Address1;
            updateduser.Address2    = Input.Address2;
            updateduser.City        = Input.City;
            updateduser.PostCode    = Input.PostCode;
            updateduser.PhoneNumber = Input.PhoneNumber;

            // Process the avatar
            if (Input.AvartarUrl != null && Input.AvartarUrl.Length > 0 && updateduser.AvartarUrl != Input.AvartarUrl)
            {
                //Avatar is changed

                string folderName          = HttpContext.Session.GetObject <string>(SessionType.UploadImage);
                string savedAvatarFileName = ImageManagment.SaveAvatarPicToServer(_hostEnvironment, folderName, Input.AvartarUrl);
                if (savedAvatarFileName.Length > 0)
                {
                    //Delete the old avatar on server
                    if (updateduser.AvartarUrl != null && updateduser.AvartarUrl.Length > 0)
                    {
                        ImageManagment.DeleteOldAvatar(_hostEnvironment, updateduser.AvartarUrl);
                    }
                    updateduser.AvartarUrl = savedAvatarFileName;
                }
            }


            if (!ModelState.IsValid)
            {
                await LoadAsync(updateduser);

                return(Page());
            }

            var result = await _userManager.UpdateAsync(updateduser);

            if (result.Succeeded == false)
            {
                throw new InvalidOperationException($"Unexpected error occurred on updatting profile of  '{updateduser.Id}'.");
            }

            await _signInManager.RefreshSignInAsync(user);

            StatusMessage = "Your profile has been updated";
            return(RedirectToPage());
        }
Пример #2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName    = Input.Email,
                    Email       = Input.Email,
                    Name        = Input.Name,
                    Country     = Input.Country,
                    State       = Input.State,
                    Address1    = Input.Address1,
                    Address2    = Input.Address2,
                    City        = Input.City,
                    PostCode    = Input.PostCode,
                    PhoneNumber = Input.PhoneNumber,
                    AvartarUrl  = "Avatar.jpg",
                };


                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    //Store the avatar
                    if (Input.AvartarUrl != null && Input.AvartarUrl.Length > 0)
                    {
                        string contextName         = "UPLOAD_PICS";
                        string folderName          = HttpContext.Session.GetObject <string>(contextName);
                        string savedAvatarFileName = ImageManagment.SaveAvatarPicToServer(_hostEnvironment, folderName, Input.AvartarUrl);
                        if (savedAvatarFileName.Length > 0)
                        {
                            user.AvartarUrl = savedAvatarFileName;
                            await _userManager.UpdateAsync(user);
                        }
                    }



                    _logger.LogInformation("User created a new account with password.");

                    //Update the role
                    string role = Request.Form["rdUserRole"].ToString();
                    if (role == UserType.AdminRole || role == UserType.SupplierRole ||
                        role == UserType.CustomerRole)
                    {
                        await _userManager.AddToRoleAsync(user, role);
                    }

                    // Process the avartar if have


/*                    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 },
 *                      protocol: Request.Scheme);
 *
 *                  await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
 *                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
 */
                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email }));
                    }
                    else
                    {
                        //  await _signInManager.SignInAsync(user, isPersistent: false);
                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

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