IdentityServer 3 - Using Client and Scope from DB

838 views Asked by At

I am new with IdentityServer 3 and the samples and tutorial are using InMemory Users, Client and Scope, but I need these to be from DB. So I did:

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

But in the ClientStore and ScopeStore I have to be mapping from my Client/Scope DB Model to IdentityServer3.Core.Models Client/Scope. Like this:

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

Is there a better way to do this, knowing that my DB Models are just a copy from these IdentityServer3.Core.Models?

2

There are 2 answers

1
Hoang Phan On BEST ANSWER

Have you tried to use AutoMapper >> https://automapper.org/

1
Brock Allen On
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);

This style is registering those services as singletons. I don't know if that's what you want.

If you want a new instance each time they're used, then use this:

factory.UserService = new Registration<IUserService, YourUserService>();