Search code examples
c#asp.net.net-coremiddlewarejwt

.net core 3 middleware or authorization attribute ? and how to?


i'm on

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

I have a multi-Database project. User choose a database from list, in the login form. After that i set the dbName/connectionString in the "selectedDb" claim

In every controller i have 8 to 20 classes (manager) that needs DbContext as parameter contructor => i can't create an istance of managers or dbContext in the controller constructor, because i do not have the login token yet!

So that , in every Action i create an istance of dbContext(token provide connection string) and managers istance... But it means i have to "copy/paste" the same 3/4 line of code in every action

how can i provide a valid instance of db context? maybe using middleware or custom authorization attribute

Is there any way to create instance of dbcontext in controller constructor ? (with "dynamic connection string" provided from token)

Some code example

Init classes function (avoid to copy 16&3#92 lines every times)

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);
}

Some Api Example

[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
}

Solution

  • With @Ruard van Elburg help and His Answer

    here the solution

    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
    }