Asp net core identity server 4 oidc при выходе logoutId равен null
в методе выхода identity server logoutId равен null не понимаю в чем проблема конфигурация identity сервера
`public static class Configuration
{
public static IEnumerable<IdentityResource> IdentityResources =>
new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
};
public static IEnumerable<ApiResource> ApiResources =>
new List<ApiResource>
{
new ApiResource("ToDo", "Web API")
{
Scopes = { "ToDo" },
},
new ApiResource("Roles", "Roles")
{
UserClaims = { JwtClaimTypes.Role },
Scopes = { "Roles" },
}
};
public static IEnumerable<ApiScope> ApiScopes =>
new List<ApiScope>
{
new ApiScope("ToDo"),
new ApiScope("Roles")
};
public static IEnumerable<Client> Clients =>
new List<Client>
{
new Client
{
ClientId = "ToDoClient",
AllowedGrantTypes = GrantTypes.Code,
RequireClientSecret = false,
RequirePkce = true,
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.OpenId,
"ToDo",
"Roles",
},
AllowedCorsOrigins =
{
"http://localhost:3000"
},
AllowAccessTokensViaBrowser = true,
RedirectUris = { "http://localhost:3000/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:3000/signout-oidc" }
},
new Client
{
ClientId = "ToDoMVC",
AllowedGrantTypes = GrantTypes.Code,
RequireClientSecret = false,
RequirePkce = true,
AccessTokenLifetime = 120,
AllowOfflineAccess = true,
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.OpenId,
"ToDo",
},
RedirectUris = {"https://localhost:44338/signin-oidc"},
PostLogoutRedirectUris = {"https://localhost:44338/signout-callback-oidc"},
}
};
}
файл program identity сервера
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
var connectionsString = builder.Configuration.GetConnectionString("ToDoConnectionString");
builder.Services.AddDbContext<AuthDbContext>(options =>
{
options.UseSqlServer(connectionsString);
});
builder.Services.AddIdentity<AppUser, IdentityRole>(options =>
{
options.Password.RequiredLength = 3;
options.Password.RequireLowercase = false;
options.Password.RequiredUniqueChars = 1;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = false;
})
.AddEntityFrameworkStores<AuthDbContext>()
.AddDefaultTokenProviders();
builder.Services.ConfigureApplicationCookie(options =>
{
options.Cookie.Name = "ToDo.Identity.Cookie";
options.LoginPath = "/Auth/Login";
options.LogoutPath = "/Auth/Logout";
});
builder.Services.AddIdentityServer()
.AddAspNetIdentity<AppUser>()
.AddInMemoryIdentityResources(Configuration.IdentityResources)
.AddInMemoryApiResources(Configuration.ApiResources)
.AddInMemoryApiScopes(Configuration.ApiScopes)
.AddInMemoryClients(Configuration.Clients)
.AddDeveloperSigningCredential();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseIdentityServer();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Auth}/{action=Login}/{id?}");
app.Run();
метод выхода identity сервер
public async Task<IActionResult> Logout(string logoutId)
{
await _signInManager.SignOutAsync();
var logoutRequest = await _interactionService.GetLogoutContextAsync(logoutId);
if (string.IsNullOrEmpty(logoutRequest.PostLogoutRedirectUri))
{
return RedirectToAction("Login");
}
return Redirect(logoutRequest.PostLogoutRedirectUri);
}
файл program MVC клиента
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
builder.Services.AddSingleton<TokenService>();
HttpClient ToDoApi = new HttpClient { BaseAddress = new Uri("https://localhost:44355") };
builder.Services.AddHttpContextAccessor();
builder.Services.AddAuthentication(config =>
{
config.DefaultScheme = "Cookie";
config.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookie")
.AddOpenIdConnect("oidc", config =>
{
config.Authority = "https://localhost:44385";
config.ClientId = "ToDoMVC";
config.ResponseType = "code";
config.UsePkce = true;
config.SaveTokens = true;
config.Scope.Add("openid");
config.Scope.Add("profile");
config.Scope.Add("ToDo");
config.UseTokenLifetime = true;
config.Events = new OpenIdConnectEvents
{
// Вызывается, когда токен был успешно проверен и декодирован.
OnTokenValidated = async context =>
{
// Получаем access token из контекста аутентификации.
var accessToken = context.SecurityToken as JwtSecurityToken;
// Получаем значение claim "exp" (время истечения токена).
var expirationTime = int.Parse(accessToken.Claims.Single(c => c.Type == "exp").Value);
// Получаем текущее время.
var currentTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
// Количество секунд до истечения токена.
var secondsUntilExpiration = expirationTime - currentTime;
// Проверяем, сколько времени осталось до истечения токена.
if (secondsUntilExpiration < 40)
{
var serviceProvider = builder.Services.BuildServiceProvider();
serviceProvider.GetService<TokenService>().RefreshToken();
}
}
};
// config.Events.OnRedirectToIdentityProviderForSignOut = context =>
// {
// var idTokenHint = context.HttpContext.User.FindFirst("id_token")?.Value;
//
// if (!string.IsNullOrEmpty(idTokenHint))
// {
// context.ProtocolMessage.IdTokenHint = idTokenHint;
// }
//
// return Task.CompletedTask;
// };
});
builder.Services.AddSingleton<HttpClient>(ToDoApi);
builder.Services.AddSingleton<ToDoService>();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
метод выхода MVC клиента
public IActionResult Exit()
{
return SignOut("Cookie", "oidc");
}
и на всякий случай файл program API
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddPersistance(builder.Configuration);
builder.Services.AddApplication();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyHeader();
policy.AllowAnyMethod();
policy.AllowAnyOrigin();
});
});
builder.Services.AddAuthentication(config =>
{
config.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
config.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer("Bearer", options =>
{
options.Authority = "https://localhost:44385/";
options.Audience = "ToDo";
options.RequireHttpsMetadata = false;
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseCors("AllowAll");
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
Источник: Stack Overflow на русском