2016-12-30 53 views
2

我是新与IdentityServer 3和样品和教程使用InMemory用户,客户和适用范围,但我需要这些从DB。所以我做:IdentityServer 3 - 使用客户端及适用范围从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我必须从我的客户端/范围DB模型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); 
} 

有没有更好的方式来做到这一点,知道我的数据库模型只是从这些IdentityServer3.Core.Models副本?

回答

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>();

+0

嗡嗡声,没有。我不想说...... – Stel

+0

更新建议是你可以想。 –