2016-12-30 54 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

更新建議是你可以想。 –