Openiddict-core: Question: Using Mongodb

Created on 15 Jan 2017  路  12Comments  路  Source: openiddict/openiddict-core

I'm using AspNet.Identity.Mongo (https://github.com/g0t4/aspnet-identity-mongo) to handle the Identity framework.

Checking the OpenIddict.EntityFrameworkCore.Stores they implemented the IOpenIddictApplicationStore, IOpenIddictAuthorizationStore, IOpenIddictScopeStore, and IOpenIddictTokenStore.

Does this mean I need to do the following in order to make it work with MongoDB?

  • Implement IOpenIddictApplicationStore, IOpenIddictAuthorizationStore, IOpenIddictScopeStore, IOpenIddictTokenStore, and custom OpenIddictApplicationManager using MongoDB
  • Is it correct to use services.AddOpenIddict?

    • Or should I register it by AddApplicationStore< CustomApplicationStore >, AddAuthorizationStore< CustomAuthorizationStore >, AddScopeStore< CustomScopeStore >, and AddTokenStore< CustomTokenStore >?

question

Most helpful comment

Does anyone have any samples of the custom implementation for mongo?

All 12 comments

Implement IOpenIddictApplicationStore, IOpenIddictAuthorizationStore, IOpenIddictScopeStore, IOpenIddictTokenStore

Yes.

and custom OpenIddictApplicationManager using MongoDB

No.

Is it correct to use services.AddOpenIddict?

No. The generic arguments should point to the models you use, not the stores.

Or should I register it by AddApplicationStore< CustomApplicationStore >, AddAuthorizationStore< CustomAuthorizationStore >, AddScopeStore< CustomScopeStore >, and AddTokenStore< CustomTokenStore >?

Yes.

Thanks!

@akeno379 Hi Were you able to implement this solution using MongoDB AspNet.Identity.Mongo. Let me know. If you could share code or insights. thanks a much

Does anyone have any samples of the custom implementation for mongo?

@aporquez Can you show your sample for the solution of mongo ?

OpenIddict RC3 now officially supports MongoDB. You can read the announcement here: https://github.com/openiddict/openiddict-core/issues/610.

This looks awesome, does anybody have a code sample for using this?

@Myrmex there's no sample yet, but the change is really trivial, as indicated in https://github.com/openiddict/openiddict-core/issues/610:

  • Reference OpenIddict.MongoDb and remove the OpenIddict.EntityFrameworkCore dependency.

  • Remove options.UseEntityFrameworkCore() and use options.UseMongoDb() instead:

services.AddOpenIddict()

    // Register the OpenIddict core services.
    .AddCore(options =>
    {
        // Configure OpenIddict to use the MongoDB stores and models.
        options.UseMongoDb()
               .UseDatabase(new MongoClient().GetDatabase("openiddict-db"));
    });
  • Replace the OpenIddict.EntityFrameworkCore.Models usings by OpenIddict.MongoDb.Models.

Thanks, but I'm missing something obvious here in my configuration. If you start from this dummy sample: https://github.com/Myrmex/oid-credentials, and do the following:

1.add OpenIddict.MongoDb and remove OpenIddict.EntityFrameworkCore.
2.remove Models/ApplicationDbContext.
3.remove the seed service (will have to be refactored for MongoDB) from Services/DatabaseInitializer and delete its references in Startup.cs.
4.in Startup.cs, remove options.UseEntityFrameworkCore() and use options.UseMongoDb() instead.

this leaves Startup.cs as follows; yet, where do I configure the identity models? In fact, when I try getting the token I get an InvalidOperationException: Unable to resolve service for type Microsoft.AspNetCore.Identity.IUserStore1[OidCredentials.Models.ApplicationUser] while attempting to activate Microsoft.AspNetCore.Identity.AspNetUserManager1[OidCredentials.Models.ApplicationUser].

using AspNet.Security.OpenIdConnect.Primitives;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
using OidCredentials.Models;
using OidCredentials.Services;
using Swashbuckle.AspNetCore.Swagger;

namespace OidCredentials
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            services.AddMvc()
                .AddJsonOptions(options =>
                {
                    options.SerializerSettings.ContractResolver =
                        new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
                });

            // Register the Identity services.
            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddDefaultTokenProviders();

            services.Configure<IdentityOptions>(options =>
            {
                options.ClaimsIdentity.UserNameClaimType = OpenIdConnectConstants.Claims.Name;
                options.ClaimsIdentity.UserIdClaimType = OpenIdConnectConstants.Claims.Subject;
                options.ClaimsIdentity.RoleClaimType = OpenIdConnectConstants.Claims.Role;
            });

            services.AddOpenIddict()
                .AddCore(options =>
                {
                    options.UseMongoDb()
                        .UseDatabase(new MongoClient().GetDatabase("oid-test"));
                })
                .AddServer(options =>
                {
                    options.UseMvc();
                    options.EnableTokenEndpoint("/connect/token");
                    options.AllowPasswordFlow();
                    options.AllowRefreshTokenFlow();
                    options.DisableHttpsRequirement();
                }).AddValidation();

            // seed the database
            // services.AddTransient<IDatabaseInitializer, DatabaseInitializer>();

            // swagger
            // https://github.com/domaindrivendev/Swashbuckle.AspNetCore
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Title = "Test API",
                    Version = "v1"
                });
            });
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env
            /*IDatabaseInitializer databaseInitializer*/)
        {
            if (env.IsDevelopment()) app.UseDeveloperExceptionPage();

            // CORS
            // https://docs.asp.net/en/latest/security/cors.html
            app.UseCors(builder =>
                builder.WithOrigins("http://localhost:4200")
                    .AllowAnyHeader()
                    .AllowAnyMethod());
            app.UseAuthentication();
            app.UseMvcWithDefaultRoute();

            // seed the database
            // databaseInitializer.Seed().GetAwaiter().GetResult();

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Test API V1");
            });
        }
    }
}

As you figured out, this is an exception thrown by the DI stack and that indicates that Identity was unable to find a user store: it has nothing to do with OpenIddict.

If you want to use Identity with MongoDB, you'll have to use a package developed by the community, like https://github.com/alexandre-spieser/AspNetCore.Identity.MongoDbCore or https://github.com/tugberkugurlu/AspNetCore.Identity.MongoDB

Thanks, I created a MongoDB version of my dummy API sample at https://github.com/Myrmex/oid-credentials-mongo.

Hi, you can see my working example openiddict 3.0.0 with mongodb and Asp Net Core.Identity.MongoDb Core https://github.com/pachman/openiddict-mongo-samples

Was this page helpful?
0 / 5 - 0 ratings