我是IdentityServer 3的新手,示例和教程正在使用InMemory用户,客户端和范围,但我需要这些来自DB。所以我做了:
Startup.cs
public void Configuration(IAppBuilder app)
{
// Allow all origins
app.UseCors(CorsOptions.AllowAll);
var factory = new IdentityServerServiceFactory();
var userService = new UserService();
var clientStore = new ClientStore();
var scopeStore = new ScopeStore();
var corsService = new CorsService();
factory.UserService = new Registration<IUserService>(resolver => userService);
factory.ClientStore = new Registration<IClientStore>(resolver => clientStore);
factory.ScopeStore = new Registration<IScopeStore>(resolver => scopeStore);
factory.CorsPolicyService = new Registration<ICorsPolicyService>(resolver => corsService);
var options = new IdentityServerOptions
{
SiteName = "Embedded IdentityServer",
SigningCertificate = LoadCertificate(),
Factory = factory
};
app.UseIdentityServer(options);
}
但是在ClientStore和ScopeStore中,我必须从我的客户端/范围数据库模型映射到IdentityServer3.Core.Models客户端/范围。像这样:
ClientStore.cs
public Task<Client> FindClientByIdAsync(string clientId)
{
var clientFromDb = _db.Clients.SingleOrDefault(x => x.ClientId == clientId);
var client = new Client
{
ClientName = clientFromDb.ClientName,
ClientId = clientFromDb.ClientId,
AccessTokenType = clientFromDb.AccessTokenType,
Enabled = clientFromDb.Enabled,
Flow = clientFromDb.Flow,
RedirectUris = clientFromDb.RedirectUris.Select(x => x.Uri).ToList(),
PostLogoutRedirectUris = clientFromDb.PostLogoutRedirectUris.Select(x => x.Uri).ToList(),
AllowedCorsOrigins = clientFromDb.AllowedCorsOrigins.Select(x => x.Origin).ToList(),
AllowedScopes = clientFromDb.AllowedScopes.Select(x => x.Scope).ToList(),
AllowAccessToAllScopes = clientFromDb.AllowAccessToAllScopes,
AccessTokenLifetime = clientFromDb.AccessTokenLifetime
};
return Task.FromResult(client);
}
有没有更好的方法来做到这一点,知道我的DB模型只是这些IdentityServer3.Core.Models的副本?
答案 0 :(得分:0)
factory.UserService = new Registration<IUserService>(resolver => userService);
factory.ClientStore = new Registration<IClientStore>(resolver => clientStore);
factory.ScopeStore = new Registration<IScopeStore>(resolver => scopeStore);
factory.CorsPolicyService = new Registration<ICorsPolicyService>(resolver => corsService);
这种风格将这些服务注册为单身人士。我不知道这是不是你想要的。
如果您希望每次使用新实例,请使用以下命令:
factory.UserService = new Registration<IUserService, YourUserService>();
答案 1 :(得分:0)
您是否尝试过使用AutoMapper >> https://automapper.org/