各位,我需要帮助。我正在一个新项目中实现Identity Server 4,尝试从数据库中恢复之前使用asp.Identity创建的用户,以便在使用Identity Server 4进行外部登录时进行验证。抱歉我的英语不好。
这是我的Startup.cs:
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
#region --Identity ASP
services.AddDbContext<QGoodDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")),ServiceLifetime.Transient);
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<QGoodDbContext>()
.AddDefaultTokenProviders();
#endregion
services.AddMvc();
services.AddScoped<UserManager<ApplicationUser>>();
services.AddScoped<SignInManager<ApplicationUser>>();
// configure identity server with in-memory stores, keys, clients and scopes
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients());
//.AddAspNetIdentity<ApplicationUser>();
}
}
当我初始化数据库时,我已经遇到了这个错误。
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseIdentityServer();
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
InitializeDatabase(app);
}
private void InitializeDatabase(IApplicationBuilder app)
{
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
//serviceScope.ServiceProvider.GetRequiredService<QGoodDbContext>().Database.Migrate();
var context = serviceScope.ServiceProvider.GetRequiredService<QGoodDbContext>();
}
}
在我的 AccountController
中,当调用 await _userManager.FindByLoginAsync(provider, providerUserId);
时会出现异常。
public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly QGoodDbContext _context;
private readonly TestUserStore _users;
private readonly IIdentityServerInteractionService _interaction;
private readonly IClientStore _clientStore;
private readonly IAuthenticationSchemeProvider _schemeProvider;
private readonly IEventService _events;
public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
QGoodDbContext context,
IIdentityServerInteractionService interaction,
IClientStore clientStore,
IAuthenticationSchemeProvider schemeProvider,
IEventService events,
TestUserStore users = null)
{
// if the TestUserStore is not in DI, then we'll just use the global users collection
// this is where you would plug in your own custom identity management library (e.g. ASP.NET Identity)
_users = users ?? new TestUserStore(TestUsers.Users);
_userManager = userManager;
_signInManager = signInManager;
_context = context;
_interaction = interaction;
_clientStore = clientStore;
_schemeProvider = schemeProvider;
_events = events;
}
public async Task<IActionResult> ExternalLoginCallback()
{
try
{
// read external identity from the temporary cookie
var result = await HttpContext.AuthenticateAsync(IdentityServer4.IdentityServerConstants.ExternalCookieAuthenticationScheme);
if (result?.Succeeded != true)
{
throw new Exception("External authentication error");
}
// lookup our user and external provider info
var (userTest, provider, providerUserId, claims) = FindUserFromExternalProvider(result);
var user = await _userManager.FindByLoginAsync(provider, providerUserId);
}
}
}
异常信息:
找不到方法:“System.Reflection.MethodInfo Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.get_SelectAsyncMethod()”