예제 #1
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                DonutStoreUser newUser = new DonutStoreUser
                {
                    UserName    = model.UserName,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    PhoneNumber = model.PhoneNumber
                };

                IdentityResult creationResult = this._signInManager.UserManager.CreateAsync(newUser).Result;
                if (creationResult.Succeeded)
                {
                    IdentityResult passwordResult = this._signInManager.UserManager.AddPasswordAsync(newUser, model.Password).Result;
                    if (passwordResult.Succeeded)
                    {
                        var confirmationToken = _signInManager.UserManager.GenerateEmailConfirmationTokenAsync(newUser).Result;

                        confirmationToken = System.Net.WebUtility.UrlEncode(confirmationToken);
                        string     currentUrl      = Request.GetDisplayUrl();
                        System.Uri uri             = new Uri(currentUrl);
                        string     confirmationUrl = uri.GetLeftPart(UriPartial.Authority);
                        confirmationUrl += "/account/confirm?id=" + confirmationToken + "&userId=" + System.Net.WebUtility.UrlEncode(newUser.Id);
                        await this._signInManager.SignInAsync(newUser, false);

                        var emailResult = await this._emailService.SendEmailAsync(model.Email, "Welcome to the Donut Store", "<p> Thanks for signing up, " + model.Email + "!</p><p>< a href =\"" + confirmationUrl + "\">Confirm your account<a></p>", "Thanks for signing up, " + model.Email);

                        if (emailResult.Success)
                        {
                            return(RedirectToAction("Index", "Home"));
                        }
                        else
                        {
                            return(BadRequest(emailResult.Message));
                        }
                    }
                    else
                    {
                        this._signInManager.UserManager.DeleteAsync(newUser).Wait();
                        foreach (var error in passwordResult.Errors)
                        {
                            ModelState.AddModelError(error.Code, error.Description);
                        }
                    }
                }
                else
                {
                    foreach (var error in creationResult.Errors)
                    {
                        ModelState.AddModelError(error.Code, error.Description);
                    }
                }
            }


            return(View());
        }
예제 #2
0
 public IActionResult SignIn(SignInViewModel model)
 {
     if (ModelState.IsValid)
     {
         DonutStoreUser existingUser = this._signInManager.UserManager.FindByNameAsync(model.UserName).Result;
         if (existingUser != null)
         {
             Microsoft.AspNetCore.Identity.SignInResult passwordResult = this._signInManager.CheckPasswordSignInAsync(existingUser, model.Password, false).Result;
             if (passwordResult.Succeeded)
             {
                 this._signInManager.SignInAsync(existingUser, false).Wait();
                 return(RedirectToAction("Index", "Home"));
             }
             else
             {
                 ModelState.AddModelError("PasswordIncorrect", "Username or Password is incorrect.");
             }
         }
         else
         {
             ModelState.AddModelError("UserDoesNotExist", "Username or Password is incorrect.");
         }
     }
     return(View());
 }