在Asp.NET Core 2.2 + IdentityServer中,可以通过以下方法来避免使用会话cookie:
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
options.Authority = "http://localhost:5000"; // IdentityServer的URL
options.RequireHttpsMetadata = false; // 如果使用HTTP而不是HTTPS,则设置为true
options.Audience = "api1"; // 与IdentityServer中的API资源名称匹配
});
app.UseAuthentication();
public static IEnumerable GetClients()
{
return new List
{
new Client
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
}
};
}
这样配置后,将不再使用会话cookie进行身份验证,而是使用JWT令牌进行身份验证。你需要根据你的具体情况进行适当的更改和调整。