在ASP.NET Core MVC中注入ApplicationUser [英] Inject ApplicationUser in ASP.NET Core MVC

查看:123
本文介绍了在ASP.NET Core MVC中注入ApplicationUser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要 ApplicationUser 的类(来自ASP.NET Identity).该实例应为当前用户.

I have a class that requires ApplicationUser (from ASP.NET Identity). The instance should be the current user.

public class SomeClass
{
    public SomeClass(ApplicationUser user)
    {

当前,我正在做的是从Controller中注入当前用户:

Currently, what I'm doing is I inject the current user from the Controller:

var currentUser = await _userManager.GetUserAsync(User);
var instance = new SomeClass(currentUser);

现在,我想使用Microsoft提供的依赖注入.我不知道如何将 ApplicationUser 添加到服务中.它需要 User ,这是Controller的属性.

Now I want to use Dependency Injection provided by Microsoft. I can't figure out how am I going to add ApplicationUser to the services. It requires User which is a property of the Controller.

那么如何通过Microsoft提供的DI注入 ApplicationUser (当前用户的实例)?

So how do you inject ApplicationUser (instance of the current user) via DI provided by Microsoft?

推荐答案

您可以将 UserManager< ApplicationUser> IHttpContextAccessor 都注入到类的构造函数中,然后:

You can inject both UserManager<ApplicationUser> and IHttpContextAccessor to the constructor of your class, then:

public class SomeClass
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly IHttpContextAccessor _context;
    public SomeClass(UserManager<ApplicationUser> userManager,IHttpContextAccessor context)
    {
        _userManager = userManager;
        _context = context;
    }

    public async Task DoSomethingWithUser() {
        var user = await _userManager.GetUserAsync(_context.HttpContext.User);
        // do stuff
    }
}

如果您不想直接依赖 IHttpContextAccessor ,但仍想使用DI,则可以创建界面来访问您的用户:

If you don't want to take direct dependency on IHttpContextAccessor but still want to use DI, you can create interface to access your user:

public interface IApplicationUserAccessor {
    Task<ApplicationUser> GetUser();
}

public class ApplicationUserAccessor : IApplicationUserAccessor {
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly IHttpContextAccessor _context;
    public ApplicationUserAccessor(UserManager<ApplicationUser> userManager, IHttpContextAccessor context) {
        _userManager = userManager;
        _context = context;
    }

    public Task<ApplicationUser> GetUser() {
        return _userManager.GetUserAsync(_context.HttpContext.User);
    }
}

然后将其注册到DI容器中并注入 SomeClass :

Then register it in DI container and inject into SomeClass:

public class SomeClass
{
    private readonly IApplicationUserAccessor _userAccessor;
    public SomeClass(IApplicationUserAccessor userAccessor)
    {
        _userAcccessor = userAccessor;
    }

    public async Task DoSomethingWithUser() {
        var user = await _userAccessor.GetUser();
        // do stuff
    }
}

其他选项包括(如注释中所述)不注入任何内容,但需要将 ApplicationUser 作为参数传递给需要它的方法(良好的选择),并且需要在使用带有特殊的任何方法之前进行初始化Initialize(user)方法(不是很好,因为您不能确定在使用其他方法之前会调用此方法).

Other options include (as mentioned in comments) not inject anything but require passing ApplicationUser as argument to the methods which require it (good option) and require initialization before using any methods with special Initialize(user) method (not so good, because you cannot be sure this method is called before using other methods).

这篇关于在ASP.NET Core MVC中注入ApplicationUser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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