예제 #1
0
 public static SanitizedUserModel Convert(TumblrUserModel oldModel, int postCount)
 {
     return((oldModel == null) ? null : new SanitizedUserModel()
     {
         UserName = oldModel.UserName, AboutMe = oldModel.AboutMe, PostCount = postCount, ProfileImageUrl = oldModel.ProfileImageUrl
     });
 }
        public IActionResult MyProfile()
        {
            TumblrUserModel t     = _UserManager.GetUserAsync(User).Result;
            UpdateModel     model = new UpdateModel()
            {
                UserName = t.UserName, Email = t.Email, AboutMe = t.AboutMe, ProfileImageUrl = t.ProfileImageUrl
            };

            return(View("MyProfile", model));
        }
        public IActionResult AddUserToRole(string Email, string RoleTitle)
        {
            TumblrUserModel user = _UserManager.FindByEmailAsync(Email).Result;

            if (!_UserManager.GetRolesAsync(user).Result.Contains(RoleTitle))
            {
                IdentityResult result = _UserManager.AddToRoleAsync(user, RoleTitle).Result;
                if (!result.Succeeded)//Check the status of the result
                {
                    throw new Exception(result.Errors.Select(e => e.Description).Aggregate((a, b) => a + "," + b));
                }
            }

            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new TumblrUserModel {
                    UserName = Input.UserName, Email = Input.Email, AboutMe = Input.AboutMe, ProfileImageUrl = Input.ProfileImageUrl
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { 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>.");

                    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());
        }