c# - 带有 EntityFramework 的 IdentityServer 4 - 获取客户端属性?

标签 c# identityserver4

我使用带有 EntityFramework 的 Identity Server 4 进行配置。我现在想从登录页面获取当前客户端。我能够从返回的 url 参数中获取客户端 ID。然后我使用 ConfigurationDbContext 从数据库中获取客户端。

但是在客户端上,Properties 属性始终为 null,即使我在该客户端的数据库中添加了一个属性。

我如何获取客户的属性?

最佳答案

与直接使用 ConfigurationDbContext 不同,IdentityServer 项目中有一个更抽象的便捷接口(interface):即 IClientStore .此接口(interface)本身包含一个函数:FindClientByIdAsync ,可用于获取 Client . Client 包含许多属性,包括:

  • 客户姓名
  • Logo Uri
  • 属性

您可以使用 DI 获取 IClientStore 的实例(我希望您已经为 ConfigurationDbContext 执行此操作)。一旦你有了这个,只需相应地调用 FindClientByIdAsync:

var yourClient = await clientStore.FindClientByIdAsync(clientId);

使用此方法时,Properties 将按预期填充。


为了解释为什么您的原始方法没有给出预期的结果,我们需要了解 Entity Framework Core 如何处理 loading related data .简单地说,在检索 Client 时实体(这是一个不同 Client 类,与我在上面使用的类不同),导航属性(例如Properties ) 默认情况下不填充,这就是您看到 null 的原因。我强烈建议您阅读 Loading Related Data如果您对其工作原理更感兴趣,请参阅文档。

IClientStore ( ClientStore ) 的 Entity Framework Core 实现为您处理相关数据的加载。这是源代码本身的代码片段:

public Task<Client> FindClientByIdAsync(string clientId)
{
    var client = _context.Clients
        // ...
        .Include(x => x.Properties)
        .FirstOrDefault(x => x.ClientId == clientId);

    var model = client?.ToModel();

    return Task.FromResult(model);
}

Include(x => x.Properties) 负责从数据库中获取 Properties

关于c# - 带有 EntityFramework 的 IdentityServer 4 - 获取客户端属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52313348/

相关文章:

authorization - ASP Core Identity Server 4 客户端 Cors 和启动配置 Cors 之间的区别

c# - AspNet.Core,IdentityServer 4 : Unauthorized (401) during websocket handshake with SignalR 1. 0 使用 JWT 不记名 token

adfs - 带有 ADFS 4.0 的 Identityserver4,无法获取用户信息或声明

asp.net-core - Asp.Net Core中的多个JWT授权机构/发布者

c# - C++ 到 C# : Pointers & Arrays

c# - Entity Framework 核心 2.0 : Error Does not contain a definition for UseSqlServerIdentityColumn

c# - Xamarin Forms XAML OnPlatform VerticalOptions

identityserver4 - 如何将 IdentityServer4 MVC 应用程序拆分为前端 UI/API 和后端 API,以便在 DMZ 中安全托管?

c# - 使用 x :Bind 设置 GridView 项目源的 UWP 问题

c# - Microsoft ASP.NET Redis session 状态引发异常