OpenIddict 2.0.0 RC3 Final是否支持AspNetUsers表?

时间:2018-07-23 04:05:22

标签: asp.net-web-api asp.net-core openiddict

我已经按照下面的网络链接上的视频教程进行操作,并且导航到https://localhost:5001/connect/token时收到以下错误:

  

失败:Microsoft.AspNetCore.Server.Kestrel [13]         连接ID“ 0HLFGA04R3IV9”,请求ID“ 0HLFGA04R3IV9:00000001”:客户端抛出了未处理的异常   应用。 System.Data.SqlClient.SqlException(0x80131904):无效   对象名称'OpenIddictApplications'。

据我所知,新的OpenIdDict不支持通过迁移创建的AspNetUsers表。这是正确的吗?

在本教程中,作者正在使用AspNetUsers表。注意:作者使用的是OpenIddict 1.0。我正在使用2.0 RC3。我无法获取示例TodoList项目来使用AspNetusers表。是否可以使OpenIddict 2.0使用AspNetUsers表?如果是,怎么办?

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

1 个答案:

答案 0 :(得分:1)

找到答案:https://stackoverflow.com/a/46329242/4630376

结果证明,OpenIddict需要两套表:ASP和OpenIddict表。