我是.net核心世界的新手,我正在尝试设置一个identityServer。我按照https://identityserver4.readthedocs.io/en/release/quickstarts/6_aspnet_identity.html的指南进行操作,并设法使基础工作正常。我现在想要将.Net核心身份与identityServer结合使用,但它提供了一个例外,我不知道如何解决。例外:
没有配置身份验证处理程序来验证方案:idsrv
Executed action IdentityServer4.Quickstart.UI.GrantsController.Index (IdentityServer) in 22.9784ms
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[0]
An unhandled exception has occurred while executing the request
System.InvalidOperationException: No authentication handler is configured to authenticate for the scheme: idsrv
at Microsoft.AspNetCore.Http.Authentication.Internal.DefaultAuthenticationManager.<GetAuthenticateInfoAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
我曾尝试浏览github问题以解决类似问题,有些人说它与IdentityServer和Identity的加载顺序有关。但在我的情况下,他们应该是正确的?
我的Startup.cs课程:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Adds IdentityServer
services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryIdentityResources(Resources.GetIdentityResources())
.AddInMemoryApiResources(Resources.GetApiResources())
.AddInMemoryClients(Clients.Get())
.AddAspNetIdentity<IdentityUser>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseIdentity();
// Adds IdentityServer
app.UseIdentityServer();
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}
答案 0 :(得分:2)
尝试将此添加到您的app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "idsrv", // Matches the name it's looking for in the exception
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
方法:
Program
答案 1 :(得分:0)
您还需要在Configure()方法中设置cookie中间件。
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = AuthenticationSchemeConstants.DefaultCookieAuthenticationScheme,
CookieName = $"{AuthenticationSchemeConstants.CookiePrefixName}.auth",
ExpireTimeSpan = TimeSpan.FromMinutes(20),
SlidingExpiration = true,
AutomaticAuthenticate = false,
AutomaticChallenge = false
});
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = AuthenticationSchemeConstants.ExternalCookieAuthenticationScheme,
AutomaticAuthenticate = false,
AutomaticChallenge = false
});
app.UseIdentityServer();
https://identityserver4.readthedocs.io/en/release/topics/signin_external_providers.html https://github.com/IdentityServer/IdentityServer4/issues/1058
希望有所帮助
答案 2 :(得分:0)
.NET core 2.0
提供程序中的 AuthenticationOptions类不再定义AuthenticationScheme
属性。
在调用Use Provider 身份验证方法在ConfigureServices 方法中时,应配置在1.x中,AutomaticAuthenticate和AutomaticChallenge属性旨在设置在单个身份验证方案上
在2.0中,这两个属性已作为单个AuthenticationOptions实例上的标志删除,并已移至基本AuthenticationOptions类中。可以在Startup.cs的ConfigureServices方法中的AddAuthentication方法调用中配置属性:
authenticationScheme
services
.AddAuthentication(opts =>{
opts.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie("idsrv", opts =>
{
//AuthenticationScheme = "idsrv",
//AutomaticAuthenticate = true,
//AutomaticChallenge = true
})