Конфигурация:
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Identity;
using NetSpace.User.Domain.Aggregates.User;
using NetSpace.User.Persistence;
using NetSpace.User.Persistence.Sqlite.Extensions;
var builder = WebApplication.CreateBuilder(args);
#region Configure Services
var connectionString = builder.Configuration.GetConnectionString("Sqlite") ?? throw new NullReferenceException("Connection string not set.");
builder.Services.AddSqlite(connectionString);
builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<NetSpaceDbContext>()
.AddDefaultTokenProviders();
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogle(options =>
{
options.ClientId = "CLIENT_ID";
options.ClientSecret = "CLIENT_SECRET";
options.CallbackPath = "/api/v1/auth/google-callback";
options.Scope.Add("email");
options.Scope.Add("profile");
});
builder.Services.AddAuthorization();
builder.Services.AddSwaggerGen();
builder.Services.AddControllers();
builder.Services.AddOpenApi();
#endregion
var app = builder.Build();
#region Configure middlewares
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwagger();
app.UseSwaggerUI();
app.MapSwagger();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
// не помогает
//app.UseCookiePolicy(new CookiePolicyOptions
//{
// HttpOnly = Microsoft.AspNetCore.CookiePolicy.HttpOnlyPolicy.Always,
// Secure = CookieSecurePolicy.Always,
// MinimumSameSitePolicy = SameSiteMode.Strict
//});
app.MapControllers();
#endregion
app.Run();
код контроллера:
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using NetSpace.User.Domain.Aggregates.User;
namespace NetSpace.User.RestApi.Controllers.V1;
[ApiController]
[Route("api/v1/auth")]
public sealed class AuthenticationController() : ApiControllerBase
{
[HttpGet("google-login")]
public async Task<ActionResult> GoogleLogin()
{
return Challenge(GoogleDefaults.AuthenticationScheme);
}
[HttpGet("google-callback")]
public async Task GoogleCallback()
{
//var result = await signInManager.GetExternalLoginInfoAsync();
Console.WriteLine(User);
}
}
После отправки запроса на google-login и ввода данных через accounts.google.com, происходят редиректы на этот же адрес (google-login), и после появляется ошибка:
Как это исправить?