コード例 #1
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new User {
                    UserName = Input.Email, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName
                };
                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);

                    await _context.Notes.AddAsync(
                        new Note {
                        Title       = "Welcome Note",
                        Description = "we are happy you are here.",
                        Content     = "<h1 style='text-align: center; '>Welcome to Notes &#x2764;</h1><div style='text-align: center; '>This is the place to keep your everyday notes.</div>",
                        UserId      = user.Id
                    });

                    await _context.SaveChangesAsync();

                    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());
        }
コード例 #2
0
ファイル: NotesServices.cs プロジェクト: dmhai/notes
        public async Task CreateAsync(Note note)
        {
            await _context.Notes.AddAsync(note);

            await _context.SaveChangesAsync();
        }