我对IdentityServer有一些问题,我正在学习,所以我很有可能弄错了。基本上我可以运行应用程序(使用identity server 4的.net核心5应用程序)
下面是我的configureServices:
公共无效服务(IServiceCollection ConfigureServices){
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders();
services.AddDbContext<BMProAppContext>();
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>()
.AddInMemoryApiScopes(deviceclients.GetApiScopes())
.AddInMemoryClients(deviceclients.GetClients())
;
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddIdentityServerJwt()
;下面是我的配置
app.UseRouting();
app.UseAuthentication();
app.UseIdentityServer();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
endpoints.MapRazorPages();当应用程序运行时,它成功地发出一个令牌(我正在使用客户端凭据流)-然后它抛出一个错误,如下所示:
Token request success.fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware1执行请求时发生未处理的异常。System.InvalidOperationException:没有为“”持有者“”方案注册身份验证处理程序。“”注册的方案是: Identity.Application,Identity.External,Identity.TwoFactorRememberMe,Identity.TwoFactorUserId,idsrv,idsrv.external,IdentityServerJwt,IdentityServerJwtBearer。您忘记调用AddAuthentication().AddSomeAuthHandler了吗?在Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext上下文处,字符串方案)在Microsoft.AspNetCore.Authorization.Policy.PolicyEvaluator.AuthenticateAsync(AuthorizationPolicy策略处,在Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext上下文处)在IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext上下文处,在IdentityServer4.Hosting.MutualTlsEndpointMiddleware.Invoke(HttpContext上下文处,IEndpointRouter路由器,IUserSession会话,IEventService事件,IBackChannelLogoutService backChannelLogoutService处,在Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext上下文)在IdentityServer4.Hosting.BaseUrlMiddleware.Invoke(HttpContext上下文)在Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext上下文)在NSwag.AspNetCore.Middlewares.SwaggerUiIndexMiddleware.Invoke(HttpContext上下文)在NSwag.AspNetCore.Middlewares.RedirectToIndexMiddleware.Invoke(HttpContext上下文)在NSwag.AspNetCore.Middlewares.OpenApiDocumentMiddleware.Invoke(HttpContext上下文)在Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext上下文)在Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext上下文)
不确定哪里出了问题,并已寻找答案,但似乎没有任何帮助:(
任何建议都很感谢。
发布于 2021-05-04 18:05:25
您正在声明一个"Bearer"方案作为默认身份验证方案,但是您忘记了该方案的处理程序。
如果在调用AddAuthentication时使用defaultScheme参数,则设置的是默认方案名称,应用程序将在必要时尝试使用它。
您需要一个具有相同名称的注册处理程序来处理请求。当您注册处理程序时,它们也接受方案名称。如果省略此参数,将使用每个处理程序的内部默认值,例如,jwtBearer在内部使用"Bearer"。
如果您想使用jwt承载处理程序,则需要使用类似以下内容:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Audience = "https://localhost:5000/";
options.Authority = "https://localhost:5000/identity/";
})但我猜您假装使用IdentityServerJwt处理程序。
只需调用不带方案名称参数的AddIdentityServerJwt,将"IdentityServerJwt"添加为该处理程序的方案名称。这是处理程序在内部分配的默认名称。
services.AddAuthentication()
.AddIdentityServerJwt();通过指定方案名称,您可以随心所欲地命名方案:
services.AddAuthentication()
.AddIdentityServerJwt("MySchemeName");请注意,如果您不需要保护身份服务上的资源,则身份服务不需要此处理程序,也就是说,如果您希望除了作为身份服务之外,它还充当受其自身保护的API资源。
还有一个问题..。根据IdentityServer4文档:UseIdentityServer includes a call to UseAuthentication,因此没有必要同时拥有两者。
何时在AddAuthentication(string defaultScheme)中添加默认方案
添加默认方案时:
如果未指定,则必须使用Authorize属性来指示用于构建用户身份的一个或多个方案:
[Authorize(AuthenticationSchemes = "MyScheme")]https://stackoverflow.com/questions/67382008
复制相似问题