如何在身份服务中更改默认令牌提供者

时间:2020-04-20 08:25:24

标签: asp.net-core asp.net-identity ef-core-3.0

我有2个使用asp.net core 3.1的项目API和Auth Server。在API项目中,我使用身份服务器4令牌验证来验证令牌。我也在API项目中使用身份。

以下用于api项目中令牌验证的代码

 var authAuthority = "https://localhost:5000";   // Configuration["BlazorBoilerplate:IS4ApplicationUrl"].TrimEnd('/');

       var authBuilder = services.AddAuthentication(options =>
        {
            options.DefaultScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme;
        })
       .AddIdentityServerAuthentication(options =>
       {
           options.Authority = authAuthority;
           options.SupportedTokens = SupportedTokens.Jwt;
           options.RequireHttpsMetadata = false;
           options.ApiName = "weatherapi";
       });

my user claims

令牌验证后,用户声明显示在httpContext.User.Identity对象中。请参考屏幕截图。

如果我将身份服务添加到ConfigureServices类中的以下代码。

public void ConfigureServices(IServiceCollection services)
        {

            services.AddIdentity<User, Role>()
          .AddRoles<Role>()
          .AddEntityFrameworkStores<CBSContext>()
          .AddDefaultTokenProviders();
        }

添加上述asp.net身份服务后,AuthorizationHandlerContext显示为空。.身份服务与身份服务器令牌验证之间是否存在任何关系。

  1. 身份验证服务器中的用户身份验证---完美运行
  2. 我需要在API项目中使用身份的用户/角色管理器。 我只想更改services.AddDefaultIdentity到我的身份服务器jwt验证中,该怎么做?

    请提出建议。

1 个答案:

答案 0 :(得分:1)

*>添加了上述asp.net身份服务后,

httpContext.User.Identity(气候)显示为空。是否有 身份服务与身份服务器令牌之间的关系 验证。请提出建议.. * 是: 参考此图片:https://identityserver4.readthedocs.io/en/latest/_images/middleware.png (提醒https://identityserver4.readthedocs.io/en/latest/_images/protocols.png

添加时:

services.AddIdentity<User, Role>() .AddRoles<Role>() .AddEntityFrameworkStores<CBSContext>() .AddDefaultTokenProviders();

对于您的API,您还说您自己存储令牌。请记住,身份验证和授权是两个不同的概念(有关更多信息:Big Picture

您可以在API中对i.ex使用身份验证。验证呼叫者是谁(身份)。但是,请在其他验证的服务器上运行授权服务器。

但是,当您编写该代码时,是说您将令牌存储在该DBContext中。

您可以拥有Authz。和Authn。在您的API上,并用它来表示客户登录和声明等。但是您也可以说您依赖于其他服务器进行Authz。部分,以便您联合该支票。 (很抱歉,我的第一个回答是很长的回答)