在ASP.NET Core MVC中注入ApplicationUser

编程入门 行业动态 更新时间:2024-10-18 22:37:20
本文介绍了在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

本文发布于:2023-11-14 04:20:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586198.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   ASP   Core   ApplicationUser   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!