Search code examples
c#mysqlasp.net-coreasp.net-identityidentityserver4

How to get users from a existing database for identityServer4


i try to understand how i can bind users (email, password, firstname, lastname and os on) which are stored in an existing database (located: localhost:3306) into my identityserver4 project so that i can use these information to login a user or register a new user into that database?

I read some tutorials (specially http://docs.identityserver.io/en/release/quickstarts/8_entity_framework.html) but i think this is always for db in the same project. my db isn´t in the same project.

In this context i read about asp.net-core Identity. but i don´t understand completely how that´s related.

Can someone tell me how can i bind a db in my project and what´s the role of identity with application User and so on?

thanks in advance


Solution

  • This article is more relevant to your situation. The one you linked is for configuration data and not for user data: http://docs.identityserver.io/en/release/quickstarts/6_aspnet_identity.html

    In short, you want to access your user data through Asp.Net Core Identity. You need to:

    • Make a user class containing the relevant fields as your database
    • Create an EntityFramework DbContext class to map your database to your class
    • Register your user class and dbcontext with aspnet core identity
    • Tell IdentityServer to use AspNetIdentity

    This is what your Startup ConfigureServices method might look like once implemented. Not pictured here is the DbContext and User classes you need to make.

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<YourUserStoreDbContextHere>(options =>
                options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
    
        services.AddIdentity<YourUserClassHere, YourRoleClassHereIfAny>()
            .AddEntityFrameworkStores<YourUserStoreDbContextHere>()
            .AddDefaultTokenProviders();
    
        services.AddIdentityServer()
            // Other config here
            .AddAspNetIdentity<YourUserClassHere>();
    }
    

    Refer to the docs on AspNet Identity for details on configuring your user class and dbcontext: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity