【问题标题】:DbContext.Database.Migrate() not providing password to the backendDbContext.Database.Migrate() 未向后端提供密码
【发布时间】:2021-05-17 08:34:29
【问题描述】:

我的目标是在创建数据库时创建用户种子。 我正在使用 idserver4、npgsql、docker-compose。 当前行为创建数据库以及身份服务器用户管理器表(AspNetUsers、AspNetUserTokens、AspNetUserRoles 等)。所以我知道它正在将数据迁移到数据库。但是它跳过了运行用户种子的任务,因为它抛出了一个密码异常:

Npgsql.NpgsqlException (0x80004005): No password has been provided but the backend requires one (in MD5)

这是我的 Program.cs 中的代码。

    public static void Main(string[] args)
    {
        var host = CreateHostBuilder(args).Build();

        using (var scope = host.Services.CreateScope())
        {
            var services = scope.ServiceProvider;
            try
            {
                var userManager = services.GetRequiredService<UserManager<User>>();
                var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
                var context = services.GetRequiredService<ApplicationDbContext>();

                context.Database.Migrate(); // ERROR HAPPENS HERE

                Task.Run(async () => await UserAndRoleSeeder.SeedUsersAndRoles(roleManager, userManager)).Wait(); // I NEED THIS TO RUN
            }
            catch (Exception ex)
            {
                var logger = services.GetRequiredService<ILogger<Program>>();
                logger.LogError(ex, "Error has occured while migrating to the database.");
            }
        }
        host.Run();
    }

这是它在 Startup.cs 中获取连接字符串的代码:

services.AddDbContext<ApplicationDbContext>(options =>
{
  options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"),
  b =>
  {
    b.MigrationsAssembly("GLFManager.App");
  });
});

如果我在这里使用断点,则表明连接字符串是与用户 ID 和密码一起获得的。我验证了密码是正确的。否则我认为它最初不会提交 Idserver 用户管理器表。

这是连接字符串所在的 appsettings.json 文件:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "Host=localhost;Port=33010;Database=glfdb;User Id=devdbuser;Password=devdbpassword"
  }
}

我认为它位于 docker-compose 文件中的某个配置未注册的位置。这是 docker-compose 文件:

version: '3.4'

services:
  glfmanager.api:
    image: ${DOCKER_REGISTRY-}glfmanagerapi
    container_name: "glfmanager.api"
    build:
      context: .
      dockerfile: GLFManager.Api/Dockerfile
    ports:
      - "33000:80"
      - "33001:443"
    environment:
      - ConnectionStrings__DefaultConnection=Server=glfmanager.db;Database=glfdb;User Id=devdbuser:password=devdbpassword;
      - Identity_Authority=http://glfmanager.auth
    volumes:
      - .:/usr/src/app
    depends_on:
      - "glfmanager.db"

  glfmanager.auth:
    image: ${DOCKER_REGISTRY-}glfmanagerauth
    container_name: "glfmanager.auth"
    build:
      context: .
      dockerfile: GLFManager.Auth/Dockerfile
    ports:
      - "33005:80"
      - "33006:443"
    environment:
      - ConnectionStrings__DefaultConnection=Server=glfmanager.db;Database=glfdb;User Id=devdbuser:password=devdbpassword;
    volumes:
      - .:/usr/src/app
    depends_on:
      - "glfmanager.db"

  glfmanager.db:
    restart: on-failure
    image: "mdillon/postgis:11"
    container_name: "glfmanager.db"
    environment:
      - POSTGRES_USER=devdbuser
      - POSTGRES_DB=glfdb
      - POSTGRES_PASSWORD=devdbpassword
    volumes:
      - glfmanager-db:/var/lib/postresql/data
    ports:
      - "33010:5432"


volumes:
  glfmanager-db:

我在后端开发的课程中使用了这段代码,该代码与我在其中构建的项目相同,并且可以正常工作。所以我很困惑为什么这给了我密码错误。

【问题讨论】:

    标签: docker .net-core docker-compose entity-framework-core identityserver4


    【解决方案1】:

    发现问题。我使用了 ':' 而不是 ';'在我的用户 ID 和密码之间的 docker 文件中

    【讨论】:

      猜你喜欢
      • 2016-07-12
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多