【问题标题】:Identity Server Confusion身份服务器混乱
【发布时间】:2021-07-26 15:17:29
【问题描述】:

我在使用 IdentityServer 时遇到问题,我正在学习它,所以很有可能我出了点问题。基本上我可以运行应用程序(.net core 5 应用程序和身份服务器 4)

下面是我的 configureServices:

public void ConfigureServices(IServiceCollection 服务) {

        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.

失败:Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] 执行请求时发生未处理的异常。 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 策略,HttpContext 上下文) 在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext 上下文) 在 IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext 上下文,IEndpointRouter 路由器,IUserSession 会话,IEventService 事件,IBackChannelLogoutService backChannelLogoutService) 在 IdentityServer4.Hosting.MutualTlsEndpointMiddleware.Invoke(HttpContext 上下文,IAuthenticationSchemeProvider 方案) 在 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 上下文)

不确定出了什么问题并已搜索答案,但似乎没有任何帮助:(

任何建议表示赞赏。

【问题讨论】:

    标签: identityserver4


    【解决方案1】:

    您将"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)中添加默认方案?

    添加默认方案时:

    • 任何尝试执行身份验证操作(Authenticate、Forbid、Challenge、SignIn 或 SignOut)而不隐式指定方案,都将使用默认方案。
    • 当用户通过多个方案进行身份验证时,HttpContext.User 将设置为默认方案解析的标识。

    如果没有指定,我们必须使用Authorize属性来指示应该用来构造用户身份的方案或方案:

    [Authorize(AuthenticationSchemes = "MyScheme")]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-25
      • 2011-02-07
      • 1970-01-01
      • 2011-11-04
      • 2012-01-19
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多