在ASP.NET Core中的GetUserManager的替代方法

编程入门 行业动态 更新时间:2024-10-24 16:30:51
本文介绍了在ASP.NET Core中的GetUserManager的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将现有的Asp Mvc项目转换为.Net Core,现在的问题是此get方法

public ApplicationUserManager UserManager { get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); } private set { _userManager = value; } }

在Mvc中,我们可以使用

来获取OwinContext

HttpContext.GetOwinContext().GetUserManager()

但是在不存在.Net Core GetOwinContext的情况下,当我比较两个类的HttpContext的定义时

在Mvc中,HttpContextBase是类型,但是在Core中,HttpContext与HttpContextBase在同一命名空间中没有扩展方法,这就是为什么找不到此扩展方法的原因,请让我知道如何在Core中做到这一点? >

解决方案

没有其他选择. ASP.NET Core几乎所有内容都使用依赖项注入.如果需要UserManager<TUser>,则可以将您正在使用的任何类的构造器(控制器等)注入到构造器中:

public class MyController : Controller { private readonly UserManager<ApplicationUser> _userManager; public MyController(UserManager<ApplicationUser> userManager) { _userManager = userManager; } ... }

I am converting an existing Asp Mvc project to .Net Core now the issue is this get method

public ApplicationUserManager UserManager { get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); } private set { _userManager = value; } }

In Mvc we could fetch OwinContext using

HttpContext.GetOwinContext().GetUserManager()

but in case of .Net Core GetOwinContext not present , When I compare definition of HttpContext of both classes

HttpContextBase is type in case of Mvc but in case of Core there is HttpContext which has no extension method in same namespace as HttpContextBase that's why this extension method was not found ,Please let me know how to do this in Core?

解决方案

There is no alternate. ASP.NET Core uses dependency injection for pretty much everything. If you need UserManager<TUser>, you inject into the constructor of whatever class you're working with (controller, etc.):

public class MyController : Controller { private readonly UserManager<ApplicationUser> _userManager; public MyController(UserManager<ApplicationUser> userManager) { _userManager = userManager; } ... }

更多推荐

在ASP.NET Core中的GetUserManager的替代方法

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

发布评论

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

>www.elefans.com

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