>你好,
我正在使用身份服务器 4,但实体框架遇到了一些问题。
我正在尝试为客户制作自定义 CRUD。尝试获取单个客户端信息时,我尝试使用实体框架(默认 IS4 实现(获取多个列表,但由于某种原因,这些列表返回为空...
在这里,可以使用快速入门 8 - 8_EntityFrameworkStorage - 并尝试访问客户端以获取其属性。
这是我当前的代码
数据库上下文
namespace IdentityServer4.EntityFramework.DbContexts
{
public class ConfigurationDbContext : DbContext, IConfigurationDbContext, IDisposable
{
public ConfigurationDbContext(DbContextOptions<ConfigurationDbContext> options, ConfigurationStoreOptions storeOptions);
public DbSet<Client> Clients { get; set; }
public DbSet<IdentityResource> IdentityResources { get; set; }
public DbSet<ApiResource> ApiResources { get; set; }
public Task<int> SaveChangesAsync();
protected override void OnModelCreating(ModelBuilder modelBuilder);
}
}
客户实体
namespace IdentityServer4.EntityFramework.Entities
{
public class Client
{
public Client();
public bool AllowOfflineAccess { get; set; }
public List<ClientScope> AllowedScopes { get; set; }
// ...
}
}
定制服务,我正在尝试获得
clnt.AllowedScopes
但是是空的...
namespace IdentityServer.Services
{
public class ClientService
{
private readonly ConfigurationDbContext _context;
private readonly ILogger _logger;
public ClientService(ILoggerFactory loggerFactory, ConfigurationDbContext context)
{
_logger = loggerFactory.CreateLogger<ClientService>();
_context = context;
}
//...
public ClientViewModel GetClientViewModel(int clientId)
{
var clnt = _context.Clients.FirstOrDefault(i => i.Id == clientId);
//todo :: return also the lists (they are coming empty)
var vm = new ClientViewModel();
if (clnt == null) return vm;
vm = new ClientViewModel()
{
Id = clnt.Id,
ClientName = clnt.ClientName,
ClientId = clnt.ClientId,
AllowedGrantTypes = clnt.AllowedGrantTypes,
ClientSecrets = clnt.ClientSecrets,
RedirectUris = clnt.RedirectUris,
PostLogoutRedirectUris = clnt.PostLogoutRedirectUris,
AllowedScopes = clnt.AllowedScopes,
AllowAccessTokensViaBrowser = clnt.AllowAccessTokensViaBrowser,
AllowOfflineAccess = clnt.AllowOfflineAccess,
AlwaysIncludeUserClaimsInIdToken = clnt.AlwaysIncludeUserClaimsInIdToken,
AlwaysSendClientClaims = clnt.AlwaysSendClientClaims,
PrefixClientClaims = clnt.PrefixClientClaims,
RequireConsent = clnt.RequireConsent
};
return vm;
}
//...
}
}
我得到了正确的客户端,它有范围(我可以在数据库上检查它并将其用于 IS 身份验证流程(,但列表是空的......
对此有什么想法吗?
干杯
将其更改为
var clnt = _context.Clients.FirstOrDefault(i => i.Id == clientId);`
这
var clnt = _context.Clients.Include(x => x.AllowedScopes)
.Include(x => x.OtherNavigationProperty)
.Include(....)
.FirstOrDefault(i => i.Id == clientId);
请参阅此处了解IdentityServer4.EntityFramework
如何做到这一点。