首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Identity Server混淆

Identity Server混淆
EN

Stack Overflow用户
提问于 2021-05-04 17:21:00
回答 1查看 71关注 0票数 0

我对IdentityServer有一些问题,我正在学习,所以我很有可能弄错了。基本上我可以运行应用程序(使用identity server 4的.net核心5应用程序)

下面是我的configureServices:

公共无效服务(IServiceCollection ConfigureServices){

代码语言:javascript
复制
        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()

        
        ;

下面是我的配置

代码语言:javascript
复制
    app.UseRouting();

        app.UseAuthentication();
        app.UseIdentityServer();
        
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller}/{action=Index}/{id?}");
            endpoints.MapRazorPages();

当应用程序运行时,它成功地发出一个令牌(我正在使用客户端凭据流)-然后它抛出一个错误,如下所示:

代码语言:javascript
复制
  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上下文)

不确定哪里出了问题,并已寻找答案,但似乎没有任何帮助:(

任何建议都很感谢。

EN

回答 1

Stack Overflow用户

发布于 2021-05-04 18:05:25

您正在声明一个"Bearer"方案作为默认身份验证方案,但是您忘记了该方案的处理程序。

如果在调用AddAuthentication时使用defaultScheme参数,则设置的是默认方案名称,应用程序将在必要时尝试使用它。

您需要一个具有相同名称的注册处理程序来处理请求。当您注册处理程序时,它们也接受方案名称。如果省略此参数,将使用每个处理程序的内部默认值,例如,jwtBearer在内部使用"Bearer"

如果您想使用jwt承载处理程序,则需要使用类似以下内容:

代码语言:javascript
复制
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        options.Audience = "https://localhost:5000/";
        options.Authority = "https://localhost:5000/identity/";
    })

但我猜您假装使用IdentityServerJwt处理程序。

只需调用不带方案名称参数的AddIdentityServerJwt,将"IdentityServerJwt"添加为该处理程序的方案名称。这是处理程序在内部分配的默认名称。

代码语言:javascript
复制
services.AddAuthentication()
   .AddIdentityServerJwt();

通过指定方案名称,您可以随心所欲地命名方案:

代码语言:javascript
复制
services.AddAuthentication()
   .AddIdentityServerJwt("MySchemeName");

请注意,如果您不需要保护身份服务上的资源,则身份服务不需要此处理程序,也就是说,如果您希望除了作为身份服务之外,它还充当受其自身保护的API资源。

还有一个问题..。根据IdentityServer4文档:UseIdentityServer includes a call to UseAuthentication,因此没有必要同时拥有两者。

何时在AddAuthentication(string defaultScheme)中添加默认方案

添加默认方案时:

  • 任何尝试在不隐式指定方案的情况下执行身份验证操作(身份验证、禁止、质询、SignIn或SignOut)的操作都将使用默认方案。
  • 当使用多个方案对用户进行身份验证时,将使用默认方案解析的标识设置HttpContext.User。

如果未指定,则必须使用Authorize属性来指示用于构建用户身份的一个或多个方案:

代码语言:javascript
复制
[Authorize(AuthenticationSchemes = "MyScheme")]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67382008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档