【问题标题】:In Identity Server 4, I have a User's identity but 0 Claims在 Identity Server 4 中,我有一个用户的身份,但有 0 个声明
【发布时间】:2018-04-24 21:14:23
【问题描述】:

我有一个身份服务器、一个 JavaScript 客户端和一个 API。所有都已配置,以便身份验证和授权按预期工作。我现在正尝试在我的 API 中通过 id(子声明)加载当前用户。问题是 User.Claims 枚举在我所有的控制器中都是空的。

我已经解码了我随每个请求发送的不记名令牌,并确认我有一个sub 声明。我的 API 中是否需要一些额外的配置来接收这些声明?

解码的 JWT

{
  "nbf": 1510436170,
  "exp": 1510439770,
  "iss": "https://localhost:44300",
  "aud": [
    "https://localhost:44300/resources",
    "mycustom.api"
  ],
  "client_id": "mycustom.web",
  "sub": "c92cc2bc-d063-49d6-a36e-f6340796df15",
  "auth_time": 1510435257,
  "idp": "local",
  "scope": [
    "openid",
    "profile",
    "mycustom.api"
  ],
  "amr": [
    "pwd"
  ]
}

API 配置

public void ConfigureServices(IServiceCollection services)
{
    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<BrewSwapDbContext>()
        .AddDefaultTokenProviders();

    services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
        .AddIdentityServerAuthentication(options =>
        {
            options.Authority = "https://localhost:44300";
            options.ApiName = "mycustom.api";
        });

    services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseCors(builder =>
        builder.AllowAnyHeader()
        .AllowAnyMethod()
        .AllowAnyOrigin());

    app.UseStaticFiles();
    app.UseAuthentication();
    app.UseMvc();
}

客户端配置

在 SPA 客户端上,我使用的是 angular-auth-oidc-client。同样,我能够检查每个请求中的不记名令牌,并且可以看到我需要的 sub 声明。请参阅上面解码的 JWT。

let oidcConfig = new OpenIDImplicitFlowConfiguration()
oidcConfig.stsServer = "https://localhost:44300";
oidcConfig.redirect_url = "https://localhost:44302";
oidcConfig.client_id = 'mycustom.web';
oidcConfig.response_type = 'id_token token';
oidcConfig.scope = 'openid profile mycustom.api';
oidcConfig.post_logout_redirect_uri = "https://localhost:44302";
oidcConfig.start_checksession = false;
oidcConfig.silent_renew = true;
oidcConfig.silent_renew_offset_in_seconds = 0;
oidcConfig.startup_route = '/home';
oidcConfig.auto_userinfo = true;
oidcConfig.log_console_warning_active = true;
oidcConfig.log_console_debug_active = true;
oidcConfig.max_id_token_iat_offset_allowed_in_seconds = 10;
oidcConfig.storage = localStorage;

API 控制器

public class ExampleController : Controller
{
    private readonly MyDbContext _context;

    public ExampleController(MyDbContext context)
    {
        _context = context;
    }

    [HttpGet]
    public IEnumerable<Example> GetExamples()
    {
        var test = User.Claims;
        return _context.Examples;
    }
}

您可以在下面看到找到了一个身份...

但它有 0 个声明。

【问题讨论】:

  • 您是否将 get 声明从用户信息端点包含到 true ?
  • app.UseOpenIdConnectAuthentication 已过时。 getClaimsFromUserInfoEndpoint 现在去哪儿了?
  • 请分享客户端配置,您是否在 IdentityServer4 端编写了任何代码来填充声明?
  • 将 [Authorize] 属性添加到 GetExample 方法时会发生什么? (只是为了确保正确设置了身份验证。)
  • @aaronR 好电话。我发现它试图重定向到我的 API 上的登录,而不是 id 服务器。与master对比发现API中的services.AddIdentity()ConfigureServices是罪魁祸首。

标签: asp.net-core jwt identityserver4


【解决方案1】:

API 设置修正:

这是您的 ConfigureServices 方法的正确代码。

不需要 AddIdentity,因为信息由来自 IdentityServer4 的声明提供。

public void ConfigureServices(IServiceCollection services)
{
     services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
        .AddIdentityServerAuthentication(options =>
        {
            options.Authority = "https://localhost:44300";
            options.ApiName = "mycustom.api";
        });

    services.AddMvc();
}

【讨论】:

  • 嗨 aaronR,我面临着类似的问题,我们在不同的应用程序中有一些 API,但在身份服务器应用程序中有一些 API。使用 .net 核心版本 1.x,用户声明随处可见,但在将 .net 核心版本升级到 2.x 后,用户声明为空。您知道在 2.x 版本中在同一应用程序中制作身份服务器和 API 是否有任何更改/限制?
  • @UtpalKumarDas 所以你有 2 个 API。一个托管在与 IDSVR 4 相同的站点,另一个托管在不同的项目/位置?
  • 嗨 aaronR 感谢您的回复,是的,我们在 IDSVR4 项目中有一些 API,其他 API 在不同的项目中,在其他项目/域中,我们收到用户声明,但是当 IDSVR 本身有资源时,问题就出现了。
  • @NastyaScherbakova 您使用的是哪个版本的 IdentityServer4?
  • @aaronR 2.5.2 版本
猜你喜欢
  • 1970-01-01
  • 2020-01-06
  • 2020-08-07
  • 2020-06-30
  • 2020-11-09
  • 1970-01-01
  • 2018-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多