Asp.net web api .NETCore2.1中CreatePerOwinContext的替代方案是什么

Asp.net web api .NETCore2.1中CreatePerOwinContext的替代方案是什么,asp.net-web-api,asp.net-core,oauth-2.0,owin,asp.net-core-2.0,Asp.net Web Api,Asp.net Core,Oauth 2.0,Owin,Asp.net Core 2.0,我有一个webapi,其中我在startup.cs文件中使用了app.CreatePerOwinContext,但我想将该webapi迁移到.net core 2.1。所以我坚持到了这一点,因为我无法为CreatePerOwinContext找到任何替代方案 以下是我的webapi代码: public static UserManager<IdentityUser> Create(IdentityFactoryOptions<UserManager<IdentityUse

我有一个webapi,其中我在startup.cs文件中使用了app.CreatePerOwinContext,但我想将该webapi迁移到.net core 2.1。所以我坚持到了这一点,因为我无法为CreatePerOwinContext找到任何替代方案

以下是我的webapi代码:

public static UserManager<IdentityUser> Create(IdentityFactoryOptions<UserManager<IdentityUser>> options, IOwinContext context)
    {
        var manager = new UserManager<IdentityUser>(new UserStore());
        return manager;
    }

 public void ConfigureAuth(IAppBuilder app)
    {

          app.CreatePerOwinContext<UserManager<IdentityUser>>(Create);
          ...
     }
publicstaticusermanager创建(IdentityFactoryOptions,IOwinContext上下文)
{
var-manager=newusermanager(newuserstore());
退货经理;
}
public void ConfigureAuth(IAppBuilder应用程序)
{
app.CreatePerOwinContext(创建);
...
}
那么如何在.net core 2.1中转换上述代码呢?

该方法被用作加载依赖项,然后在整个代码中访问它们。服务位置本身,不建议在绝大多数实际情况下使用

相反,人们现在用它来管理依赖注入。在ASP.NET MVC核心中,现在有一个轻量级的“足够好”IOC容器作为框架的一部分提供给您

Microsoft提供,但简短的版本是,在Startup.cs中,您可以在ConfigureServices下注册依赖关系树(通常使用扩展方法,以便Startup.cs不会变得太大)

注册依赖项后,可以通过属性注入、构造函数注入或方法参数注入加载它们。这导致代码比标准服务位置更干净、更易于维护

编辑:

如果您确实坚持管理服务定位器,或者是因为技术债务是可以接受的,或者是因为业务案例保证了当前的设计,那么我建议您将您的工作从OwinContext转移到HttpContext

在ASP.NET Core中,您可以通过将HttpContextAccessor注入到类中,并将OwinContext调用更改为从HttpContext中的键值存储中提取来访问HttpContext

可以找到注入HttpContextAccessor的说明。只需使用
HttpContext.Current.Application[“myObject”]
存储KVP即可


我不建议这样做,但我愿意与大家分享,因为我了解最后期限的现实与架构的理想主义。

感谢您的回答,但您能否提供一些示例代码,说明如何将OwinContext转换为HttpContext?我如何在HttpContext.Current.Application[“myObject”]中进行存储?@Ask-Yeah-man,这在我的答案中非常正确。在我的回答中,检查注入的
HttpContextAccessor
的链接,然后使用我使用的语法简单地使用应用程序KVP存储。