C# 将IdentityServer添加到具有单个用户帐户的Razor应用程序-找到多个DbContext
我创建了一个Razor Pages Web应用程序,将身份验证设置为个人用户帐户 然后,我尝试将IdentityServer4添加到启动中,以便我还可以支持{{base_url}}/connect/token来获取访问令牌 当我调用{{base_url}}/connect/token时,我得到一个500错误,控制台日志显示Clients表不存在 然后,我尝试执行添加迁移,结果出现错误:找到了多个DbContext 下面是我的Startup.cs文件。。。这里有什么我应该修正的,这样就不会(看起来)有两个DBContext了吗C# 将IdentityServer添加到具有单个用户帐户的Razor应用程序-找到多个DbContext,c#,sql-server,identityserver4,razor-pages,asp.net-core-3.1,C#,Sql Server,Identityserver4,Razor Pages,Asp.net Core 3.1,我创建了一个Razor Pages Web应用程序,将身份验证设置为个人用户帐户 然后,我尝试将IdentityServer4添加到启动中,以便我还可以支持{{base_url}}/connect/token来获取访问令牌 当我调用{{base_url}}/connect/token时,我得到一个500错误,控制台日志显示Clients表不存在 然后,我尝试执行添加迁移,结果出现错误:找到了多个DbContext 下面是我的Startup.cs文件。。。这里有什么我应该修正的,这样就不会(看起来
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using MyIdentityServer.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MyIdentityServer.Entities;
using MyIdentityServer.Services;
using Microsoft.AspNetCore.Identity.UI.Services;
using MyIdentityServer.IdentityServer;
using System.Reflection;
namespace MyIdentityServer
{
public class Startup
{
public Startup(IWebHostEnvironment environment, IConfiguration configuration)
{
Environment = environment;
Configuration = configuration;
}
public IWebHostEnvironment Environment { get; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var connectionString = Configuration.GetConnectionString("DefaultConnection");
string migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
services.AddIdentity<ApplicationUser, IdentityRole>(config =>
{
config.SignIn.RequireConfirmedEmail = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
var builder = services.AddIdentityServer(options =>
{
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;
options.EmitStaticAudienceClaim = true;
})
// .AddInMemoryPersistedGrants()
/*
.AddInMemoryIdentityResources(IdentityServerConfig.IdentityResources)
.AddInMemoryApiScopes(IdentityServerConfig.ApiScopes)
.AddInMemoryClients(IdentityServerConfig.Clients)
*/
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
options.EnableTokenCleanup = true; // this enables automatic token cleanup. this is optional.
options.TokenCleanupInterval = 30;
})
.AddAspNetIdentity<ApplicationUser>();
services.Configure<SmtpSettings>(Configuration.GetSection("SmtpSettings"));
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddScoped<IDecryptCookieService, DecryptCookieService>();
services.AddAuthentication()
.AddFacebook(facebookOptions =>
{
facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
facebookOptions.AccessDeniedPath = "/AccessDeniedPathInfo";
})
.AddTwitter(twitterOptions =>
{
twitterOptions.ConsumerKey = Configuration["Authentication:Twitter:ConsumerAPIKey"];
twitterOptions.ConsumerSecret = Configuration["Authentication:Twitter:ConsumerSecret"];
twitterOptions.RetrieveUserDetails = true;
});
services.AddControllersWithViews();
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseIdentityServer();
app.UseAuthentication();
app.UseAuthorization(); // <- allows the use of [Authorize] on controllers and action
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
});
}
}
}
好的。。。因此,IdentityServer4(如上所述实现时)确实添加了两个额外的上下文:
// add-migration -context ApplicationDbContext
// add-migration -context PersistedGrantDbContext
// add-migration -context ConfigurationDbContext
好的。。。因此,IdentityServer4(如上所述实现时)确实添加了两个额外的上下文:
// add-migration -context ApplicationDbContext
// add-migration -context PersistedGrantDbContext
// add-migration -context ConfigurationDbContext
// add-migration -context ApplicationDbContext
// add-migration -context PersistedGrantDbContext
// add-migration -context ConfigurationDbContext