未在IdentityDbContext中创建数据库连接 [英] Database connection not created in IdentityDbContext

查看:218
本文介绍了未在IdentityDbContext中创建数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Asp.Net Identity 2验证Asp.Net Core 2 Web API.这是我的AuthController和databasecontext代码:

I'm trying to authentication Asp.Net Core 2 Web API with Asp.Net Identity 2. Here's my code for the AuthController and the databasecontext:

public AuthController(UserManager<ApiUser> userManager, SignInManager<ApiUser> signInManager, RoleManager<ApiRole> roleManager
        , IPasswordHasher<ApiUser> passwordHasher, IConfiguration configurationRoot, ILogger<AuthController> logger)
    {
        _userManager = userManager;
        _signInManager = signInManager;
        _roleManager = roleManager;
        _logger = logger;
        _passwordHasher = passwordHasher;
        _configurationRoot = configurationRoot;
    }

public class SecurityContext : IdentityDbContext<ApiUser, ApiRole, int>
{
    public SecurityContext(DbContextOptions<SecurityContext> options)
        : base(options)
    {
    }
}

在运行时,当我检查SecurityContext内部的选项时,可以看到connectionstring属性集.但是,当我调试_userManager时,未设置数据库连接.

At runtime, when I inspect the options inside the SecurityContext, I can see the connectionstring property set. But when I debug the _userManager, the database connection is not set.

我还从Startup.cs配置了数据库连接:

I have configured the database connection from the Startup.cs as well:

        services.AddDbContext<SecurityContext>(options =>
 options.UseSqlServer(Configuration.GetConnectionString("SystemDbContext")));

        services.AddIdentity<ApiUser, ApiRole>()
            .AddEntityFrameworkStores<SecurityContext>()
            .AddDefaultTokenProviders();

在appsettings.json中设置了数据库连接:

Database connection is set in the appsettings.json:

{
  "ConnectionStrings": {
    "SystemDbContext": "Server=xxx;Database=xxxx;user id=sa;password=xxx;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

执行登录端点时遇到的错误是-用户域用户帐户"登录失败,而我已将SQL连接设置为与"sa"连接.似乎不是设置数据上下文的连接字符串的属性.

The error I'm getting when executing the Login endpoint is - 'Login failed for user "domain user account", whereas I have set my SQL Connection to connect with 'sa'. Seems it's not property setting the connection string of the data context.

更新: 经过进一步的调查,我发现尽管DbContextOptions的SQLConnectionString正确,但连接为空.

Update: On further investigation, I see that although the DbContextOptions has the SQLConnectionString correct, the connection is null.

有人可以指出这里缺少什么吗?预先感谢.

Can someone point out what's missing here? Thanks in advance.

推荐答案

您的ConnectionString不正确,您需要删除Trusted_Connection=True部分. 将appsettings连接字符串设置为以下内容,它将起作用

Your ConnectionString is not right, you need to drop Trusted_Connection=True part. Set appsettings connection string as the following, and it will work

{
  "ConnectionStrings": {
    "SystemDbContext": "Server=xxx;Database=xxxx;user id=sa;password=xxx;MultipleActiveResultSets=true"
  }
}

这篇关于未在IdentityDbContext中创建数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆