【问题标题】:.net core 3 middleware or authorization attribute ? and how to?.net core 3 中间件或授权属性?怎么做?
【发布时间】:2020-02-01 19:12:53
【问题描述】:

我来了

  • .net Core 3.0 web api
  • token jwt / owin
  • EF Core 3

我有一个多数据库项目。用户在登录表单中从列表中选择一个数据库。之后,我在“selectedDb”声明中设置了 dbName/connectionString

在每个控制器中,我有 8 到 20 个类(管理器)需要 DbContext 作为参数构造器 => 我无法在控制器构造器中创建管理器或 dbContext 的实例,因为我还没有登录令牌!

所以,在每个动作中,我创建了一个 dbContext(令牌提供连接字符串)和管理器的 istance...但这意味着我必须在每个动作中“复制/粘贴”相同的 3/4 行代码

如何提供一个有效的数据库上下文实例?可能使用中间件或自定义授权属性

有没有办法在控制器构造函数中创建 dbcontext 的实例? (使用令牌提供的“动态连接字符串”)

一些代码示例

初始化类函数(避免每次复制16&3#92行)

private DatabaseContext InitContextAndManager(string connectionString)
{
    _dbContext = new DatabaseContext(connectionString);

    _someManager1 = new SomeManager_1(_dbContext);
    _someManager2 = new SomeManager_2(_dbContext);
    _someManager3 = new SomeManager_3(_dbContext);
    _someManager4 = new SomeManager_4(_dbContext);
    _someManager5 = new SomeManager_5(_dbContext);
    _someManager6 = new SomeManager_6(_dbContext);
    _someManager7 = new SomeManager_7(_dbContext);
    _someManager8 = new SomeManager_8(_dbContext);
    _someManager9 = new SomeManager_9(_dbContext);
    _someManager10 = new SomeManager_10(_dbContext);
    _someManager11 = new SomeManager_11(_dbContext);
    _someManager12 = new SomeManager_12(_dbContext);
    _someManager13 = new SomeManager_13(_dbContext);
    _someManager14 = new SomeManager_14(_dbContext);
    _someManager15 = new SomeManager_15(_dbContext);
}

一些 API 示例

[Authorize]
public ActionResult Api_1()
{
    var connectionString = User.Identity.GetConnectionString();
    InitContextAndManager(connectionString);

    //some api_1 stuff
}

[Authorize]
public ActionResult Api_2()
{
    var connectionString = User.Identity.GetConnectionString();
    InitContextAndManager(connectionString);

    //some api_2 stuff
}

[Authorize]
public ActionResult Api_3()
{
    var connectionString = User.Identity.GetConnectionString();
    InitContextAndManager(connectionString);

    //some api_3 stuff
}

【问题讨论】:

  • 看看我的回答here。在 DbConnectionInfo 中,您可以访问声明并设置连接字符串。
  • 对不起,我不明白你的意思@RuardvanElburg
  • @RuardvanElburg 添加了详细信息
  • ASP.NET Core 支持依赖注入 (DI) 软件设计模式,这是一种在类及其依赖项之间实现控制反转 (IoC) 的技术。 documentation。在该模式中,您不会创建对象(new 关键字)而是注入它们。我的答案是基于 DI。

标签: c# asp.net .net-core middleware jwt-auth


【解决方案1】:

在@Ruard van Elburg 的帮助下和His Answer

解决办法

public ControllerConstructor(DbConnectionInfo db)
{
    _databaseContext = db.DbContext;
    _someManager1 = new SomeManager(_dbcontext);
}
public class DbConnectionInfo
{
    public DatabaseContext DbContext { get; set; }

    public DbConnectionInfo(IHttpContextAccessor httpContextAccessor)
    {
        var user = httpContextAccessor.HttpContext.User;
        //for question example
        DbContext = new DatabaseContext(user.Identity.GetConnectionString);
    }
}
[Authorize]
public ActionResult Api_1()
{
    //some api_1 stuff
}

【讨论】:

  • SomeManager 应该被注入到控制器中,例如public MyController(SomeManager someManager)。 SomeManager 的构造函数是:public SomeManager(SomeContext someContext),其中 SomeContext 的构造函数是:public SomeContext(DbConnectionInfo)。如果配置正确,最后一个类将由 DI 自动解析。如果您收到错误,则可能是某个服务无法解析,因为它没有添加到 DI。使用 DI 不需要 new 关键字。可以使用new 创建 DTO,但不能使用服务。在 Api_1 中:_someManager1.DoSomething();
  • 某些 Manager 1 可能只存在于控制器 1 和 3 中,某些 manager 2/3 存在于控制器 1 2 4 5 中
猜你喜欢
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 2021-07-19
  • 2021-01-12
  • 2020-07-21
  • 2019-07-31
  • 2017-05-22
相关资源
最近更新 更多