IdentityServer4 CookieAuthenticationScheme设置
在使用IdentityServer4时报错,错误如下:
ERROR|IdentityServer4.Startup|Authentication scheme Bearer is configured for IdentityServer,
but it is not a scheme that supports signin (like cookies). Either configure the default
authentication scheme with cookies or set the CookieAuthenticationScheme on the
IdentityServerOptions.
解决办法:
1、在Startup.cs中的services.AddIdentityServer添加如下代码。
services.AddIdentityServer(options=>options.Authentication.CookieAuthenticationScheme="Cookie")
services.AddIdentityServer(options=>options.Authentication.CookieAuthenticationScheme="Cookie")
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(Resources.GetIdentityResourceResources())
.AddInMemoryApiResources(Resources.GetApiResources())
.AddInMemoryClients(Clients.GetClients())
.AddResourceOwnerValidator<ResourceOwnerPasswordValidator>();
2、在services.AddAuthentication中添加AddCookie(“Cookie”);
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
options.Authority = $"http://{Configuration["Identity:IP"]}:{Configuration["Identity:Port"]}";
options.RequireHttpsMetadata = false;
options.ApiName = "WebApi";
options.ApiSecret = "DAWebApi";
}).AddCookie("Cookie");