public async Task <IActionResult> OnGetAsync(string Email, string Password, bool PersistentCookie)
        {
            var SignInUser = new User {
                id = Email, Password = Password
            };
            User DbUser = await _dbService.GetUser(SignInUser.id);

            if (DbUser != null)
            {
                if (_passwordService.VerifyPassword(SignInUser.Password, DbUser.PasswordSalt, DbUser.Password))
                {
                    var claims = new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, DbUser.Email),
                        new Claim(ClaimTypes.GivenName, DbUser.Firstname),
                        new Claim(ClaimTypes.Surname, DbUser.Lastname)
                    };

                    var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

                    AuthenticationProperties authProperties = new AuthenticationProperties
                    {
                        AllowRefresh = true,
                        ExpiresUtc   = PersistentCookie ? null : DateTime.UtcNow.AddHours(1.0d),
                        IsPersistent = PersistentCookie,
                        IssuedUtc    = PersistentCookie ? null : DateTime.UtcNow,
                        RedirectUri  = this.Request.Host.Value
                    };

                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                                                  new ClaimsPrincipal(claimsIdentity),
                                                  authProperties);
                }
                else
                {
                    Console.WriteLine("Incorrect password");
                }
            }
            else
            {
                Console.WriteLine("No such user");
            }

            return(LocalRedirect("/"));
        }
Exemple #2
0
        public async Task <IActionResult> OnGetCallbackAsync(
            string returnUrl = null, string remoteError = null)
        {
            // Get the information about the user from the external login provider
            var GoogleUser = this.User.Identities.FirstOrDefault();

            if (GoogleUser.IsAuthenticated)
            {
                var authProperties = new AuthenticationProperties
                {
                    IsPersistent = true,
                    RedirectUri  = this.Request.Host.Value
                };
                User NativeUser = new User
                {
                    id        = GoogleUser.Claims.Where(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").FirstOrDefault().Value,
                    Email     = GoogleUser.Claims.Where(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress").FirstOrDefault().Value,
                    Firstname = GoogleUser.Claims.Where(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname").FirstOrDefault().Value,
                    Lastname  = GoogleUser.Claims.Where(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname").FirstOrDefault().Value
                };

                User DbUser = await _dbService.GetUser(NativeUser.id);

                if (DbUser is null)
                {
                    NativeUser.RegisteredDateTime = DateTime.UtcNow;
                    await _dbService.AddUser(NativeUser);
                }

                await HttpContext.SignInAsync(
                    CookieAuthenticationDefaults.AuthenticationScheme,
                    new ClaimsPrincipal(GoogleUser),
                    authProperties);
            }
            return(LocalRedirect("/"));
        }