Exemplo n.º 1
0
        public async Task <IActionResult> LogInGoogle([FromBody] GoogleLogInRequest model,
                                                      [FromServices] IGoogleAuth service, CancellationToken cancellationToken)
        {
            var login = await service.LogInAsync(model.Token, cancellationToken);

            if (login is null)
            {
                return(BadRequest());
            }
            var query  = new ValidateUserQuery(login.Email);
            var result = await _queryBus.QueryAsync(query, cancellationToken);

            if (result is null)
            {
                return(Unauthorized());
            }
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, login.Email),
                new Claim("FullName", $"{login.FirstName} { login.LastName}"),
                new Claim("UserId", result.Id.ToString()),
                new Claim(ClaimsPrincipalExtensions.ClaimCountry, result.Country ?? "None"),
            };
            //foreach (var resultRole in result.Roles ?? Enumerable.Empty<string>())
            //{
            //    claims.Add(new Claim(ClaimTypes.Role, resultRole));
            //}

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

            var authProperties = new AuthenticationProperties
            {
                //AllowRefresh = <bool>,
                // Refreshing the authentication session should be allowed.

                ExpiresUtc = DateTimeOffset.UtcNow.AddHours(1),
                // The time at which the authentication ticket expires. A
                // value set here overrides the ExpireTimeSpan option of
                // CookieAuthenticationOptions set with AddCookie.

                IsPersistent = false,
                // Whether the authentication session is persisted across
                // multiple requests. When used with cookies, controls
                // whether the cookie's lifetime is absolute (matching the
                // lifetime of the authentication ticket) or session-based.

                //IssuedUtc = <DateTimeOffset>,
                // The time at which the authentication ticket was issued.

                //RedirectUri = <string>
                // The full path or absolute URI to be used as an http
                // redirect response value.
            };

            await HttpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(claimsIdentity),
                authProperties);

            return(Ok());
        }
Exemplo n.º 2
0
 public async Task ValidateUserQuery_Ok()
 {
     var query = new ValidateUserQuery("*****@*****.**");
     await _fixture.QueryBus.QueryAsync(query, default);
 }