首页 > 解决方案 > IdentityServer4 SQLite 错误 1:“没有这样的表:AspNetUserLogins”

问题描述

我正在尝试使用 IdentityServer4 并坚持使用它的内存实现。我基本上使用的是 Quickstart Samples 中的6_ASPNetIdentity

该代码在 localhost 中运行良好,但在 IIS 中将其发布到服务器后,我现在收到SQLite 错误 1:'没有这样的表:AspNetUserLogins'。

数据库文件位于应用程序的内容根文件夹中,并具有正确的文件权限。经过检查,它确实有表AspNetUserLogins 在此处输入图像描述

下面是 Startup.cs 的片段:

public Startup(IConfiguration configuration, IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();

        Configuration = builder.Build();
        Environment = env;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlite($"Data Source={Environment.ContentRootPath}/AspIdUsers.db"));

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

        services.AddMvc();

        services.Configure<IISOptions>(iis =>
        {
            iis.AuthenticationDisplayName = "Windows";
            iis.AutomaticAuthentication = false;
        });

        var builder = services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents = true;
                options.Events.RaiseSuccessEvents = true;
            })
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
            .AddAspNetIdentity<ApplicationUser>();

而在AccountController.cs中,可以在ExternalLoginCallback中跟踪错误

    /// <summary>
    /// Post processing of external authentication
    /// </summary>
    [HttpGet]
    public async Task<IActionResult> ExternalLoginCallback()
    {
        // read external identity from the temporary cookie
        var result = await HttpContext.AuthenticateAsync(IdentityConstants.ExternalScheme);
        if (result?.Succeeded != true)
        {
            throw new Exception("External authentication error");
        }

        // lookup our user and external provider info
        var (user, provider, providerUserId, claims) = await FindUserFromExternalProviderAsync(result);

这指向了这个方法:

private async Task<(ApplicationUser user, string provider, string providerUserId, IEnumerable<Claim> claims)> 
        FindUserFromExternalProviderAsync(AuthenticateResult result)
    {
        var externalUser = result.Principal;

        // try to determine the unique id of the external user (issued by the provider)
        // the most common claim type for that are the sub claim and the NameIdentifier
        // depending on the external provider, some other claim type might be used
        var userIdClaim = externalUser.FindFirst(JwtClaimTypes.Subject) ??
                          externalUser.FindFirst(ClaimTypes.NameIdentifier) ??
                          throw new Exception("Unknown userid");

        // remove the user id claim so we don't include it as an extra claim if/when we provision the user
        var claims = externalUser.Claims.ToList();
        claims.Remove(userIdClaim);

        var provider = result.Properties.Items["scheme"];
        var providerUserId = userIdClaim.Value;

        // find external user
        var user = new ApplicationUser();
        try
        {
            user = await _userManager.FindByLoginAsync(provider, providerUserId);
        }
        catch (Exception ex)
        {

            throw(ex);
        }


        return (user, provider, providerUserId, claims);
    }

尝试执行以下操作时出现异常:

user = await _userManager.FindByLoginAsync(provider, providerUserId);

有人可以请教我。我在这里想念什么?

标签: sqliteasp.net-identityasp.net-core-2.0identityserver4

解决方案


推荐阅读