簡體   English   中英

DBContext更改后如何在controller內創建新的UserManager object

[英]How to create new UserManager object inside controller after DBContext changed

I want to have a refreshed UserManager object after my DbContext changed with new connectionString inside the Controller, I have injected the UserManager in the Controller but it's obvious that it will always have the last DbContext reference from DI, not to the newly created dbcontext .

我已經嘗試過如下。

this.DbContext = new ApplicationDbContext(Configuration, optionsBuilder.Options);
this._userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(DbContext), null, new PasswordHasher<ApplicationUser>(), null, null, null, null, null, null);

它工作正常,但缺少大部分UserManager功能,如_userManager.CheckPasswordAsync(user, loginModel.Password) ,因為我將大部分參數作為 null 傳遞。 我應該怎么做才能以最簡單的方式獲得具有新DbContext的完全正常工作的UserManger

您可以嘗試創建一個服務並在服務中使用 UserManger,然后使用 Startup.ConfigureServices 方法中的 Transient 操作來配置該服務。 瞬態操作總是不同的,每次檢索服務都會創建一個新實例。 然后,您可以在 controller 中使用此服務。 請檢查以下步驟:

創建一個 UserManagerRepository 服務(在此服務中,您可以創建方法並使用 UserManager 方法):

public interface IUserManagerRepository
{
    void Write(string message);
}
public class UserManagerRepository : IUserManagerRepository, IDisposable
{
    private bool _disposed;
    private readonly UserManager<IdentityUser> _userManager;

    public UserManagerRepository(UserManager<IdentityUser> userManager)
    {
        _userManager = userManager;
    }

    public void Write(string message)
    {
        // _userManager.ChangePasswordAsync()
        Console.WriteLine($"UserManagerRepository: {message}");
    }

    public void Dispose()
    {
        if (_disposed)
            return;

        Console.WriteLine("UserManagerRepository.Dispose");
        _disposed = true;
    }
}

在 Startup.ConfigureServices 方法中使用以下代碼配置服務:

 services.AddTransient<IUserManagerRepository, UserManagerRepository>();

之后,在 controller 操作方法中手動調用服務。

    public IActionResult Index()
    {
        var services = this.HttpContext.RequestServices;
        var log = (IUserManagerRepository)services.GetService(typeof(IUserManagerRepository));

        log.Write("Index method executing");
         
        var log2 = (IUserManagerRepository)services.GetService(typeof(IUserManagerRepository));

        log2.Write("Index method executing");
        var log3 = (IUserManagerRepository)services.GetService(typeof(IUserManagerRepository));

        log3.Write("Index method executing"); 
        return View();
    }

截圖如下:

在此處輸入圖像描述

參考:

教程:在 .NET 中使用依賴注入

依賴注入指南

ASP.NET 內核中的依賴注入

使用依賴注入,這就是所有提到的工具的使用方式。

private DbContext DbContext { get;}

private UserManager<IdentityUser> UserManager { get; } 

public MyController(DbContext dbContext, UserManager<IdentityUser> userManager)
{
    DbContext = dbContext;
    UserManager = userManager;
}

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0

這是有關如何為 EF Core 設置依賴注入的頁面:

https://docs.microsoft.com/en-us/ef/core/dbcontext-configuration/#dbcontext-in-dependency-injection-for-aspnet-core

假設您已經擁有services.AddDefaultIdentity(...)services.AddDbContext(...) ,注入UserManagerYourProjectDbContext本來就應該在您的控制器中工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM