Beispiel #1
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                var user = new User
                {
                    Name           = Input.Name,
                    Surname        = Input.Surname,
                    Login          = Input.Email.Substring(0, Input.Email.IndexOf('@')),
                    Email          = Input.Email,
                    PasswordHash   = Input.Password,
                    DateRegistered = DateTime.Now
                };
                var result = await _apiClient.AddUserAsync(user);

                if (result)
                {
                    string token = await _apiClient.Authenticate(user.Login, Input.Password);

                    if (!string.IsNullOrEmpty(token))
                    {
                        HttpContext.Response.Cookies.Append("token", token.Encrypt());
                        HttpContext.Response.Cookies.Append("user", user.Email);
                        return(Redirect("~/"));
                    }
                    return(Redirect("~/Account/Login"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "User with this Email address has been already registered");
                    return(Page());
                }
            }
            // If we got this far, something failed, redisplay form
            return(Page());
        }