Search code examples
asp.net-web-apiasp.net-coreopeniddict

Does OpenIddict 2.0.0 RC3 Final support AspNetUsers table?


I have followed the video tutorial at the weblink below and am receiving the following error when I navigate to https://localhost:5001/connect/token:

fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HLFGA04R3IV9", Request id "0HLFGA04R3IV9:00000001": An unhandled exception was thrown by the application. System.Data.SqlClient.SqlException (0x80131904): Invalid object name 'OpenIddictApplications'.

From what I can tell, the new OpenIdDict does not support the AspNetUsers table that is created with a migration. Is this correct?

In this tutorial, the author is using the AspNetUsers table. Note: the author is using OpenIddict 1.0. and I am using 2.0 RC3. I am unable to get my sample TodoList project to use the AspNetusers table. Is it possible to get OpenIddict 2.0 to use the AspNetUsers table? If yes, how?

https://www.youtube.com/watch?v=GIQqIz1Gpvo&index=4&list=PLu4Bq53iqJJAo1RF0TY4Q5qCG7n9AqSZf

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using TodoListAPI.Data;
using JsonApiDotNetCore.Extensions;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using OpenIddict.Abstractions;
using OpenIddict.Core;
using OpenIddict.EntityFrameworkCore.Models;
using OpenIddict.Validation;
using TodoListAPI.Models;
using AspNet.Security.OpenIdConnect.Primitives;
using Microsoft.IdentityModel.Tokens;

//// Some code here

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

    services.AddCors(options => 
    {
        options.AddPolicy("AllowSpecificOrigins",
        builder =>
        {
            builder.WithOrigins("http://localhost:4200");
        });

        //options.AddPolicy("AllowAllOrigins",
        //builder =>
        //{
        //    builder.AllowAnyOrigin();
        //});
    });

    services.AddDbContext<AppDbContext>(opt => 
        {
            opt.UseSqlServer(this.GetConnectionString());
            opt.UseOpenIddict();
        });

    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<AppDbContext>()
        .AddDefaultTokenProviders();

    services.AddOpenIddict()
        .AddCore(opt =>
        {
            opt.UseEntityFrameworkCore()
                .UseDbContext<AppDbContext>();
        })
        .AddServer(options =>
        {
            options.UseMvc();
            options.EnableTokenEndpoint("/connect/token");
            options.AllowPasswordFlow();
            options.AllowRefreshTokenFlow();
            options.DisableHttpsRequirement();
            options.AcceptAnonymousClients();
            // options.AllowAuthorizationCodeFlow();
        })
        .AddValidation()
        ;

    services.AddAuthentication(options => {
        options.DefaultScheme = OpenIddictValidationDefaults.AuthenticationScheme;
    });

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

    services.AddJsonApi<AppDbContext>(opt => opt.Namespace = "api/v1");
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    // Shows UseCors with CorsPolicyBuilder.
    //app.UseCors(builder => builder.WithOrigins("http://localhost:4200")); 
    app.UseCors("AllowSpecificOrigins");

    //app.UseIdentity();
    //app.UseOpenIddict();
    //app.useoauth

    app.UseAuthentication();
    app.UseMvcWithDefaultRoute();
    app.UseJsonApi();
}

Solution

  • Found the answer: https://stackoverflow.com/a/46329242/4630376

    Turns out that OpenIddict requires both sets of tables: ASP and OpenIddict tables.