C# Can';无法从apicontroller中的OwinContext获取UserManager

C# Can';无法从apicontroller中的OwinContext获取UserManager,c#,asp.net-web-api,asp.net-identity,owin,C#,Asp.net Web Api,Asp.net Identity,Owin,下面是一个Microsoft示例,用于使用Identity 2.0.0实现电子邮件验证 我被这部分卡住了 public ApplicationUserManager UserManager { get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); } private set { _use

下面是一个Microsoft示例,用于使用Identity 2.0.0实现电子邮件验证

我被这部分卡住了

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

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

如何在
ApiController
中访问此
UserManager
。我想你只是缺少了一些使用语句

GetOwinContext().GetUserManager()
位于
Microsoft.AspNet.Identity.Owin

因此,请尝试添加此部分:

using Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity; // Maybe this one too

var manager = HttpContext.Current.GetOwinContext().GetUserManager<UserManager<User>>();
使用Microsoft.AspNet.Identity.Owin;
使用Microsoft.AspNet.Identity;//也许这个也是
var manager=HttpContext.Current.GetOwinContext().GetUserManager();

如果您想对控制器进行单元测试,此扩展方法可能是一个更好的解决方案

using System;
using System.Net.Http;
using System.Web;
using Microsoft.Owin;

public static IOwinContext GetOwinContext(this HttpRequestMessage request)
{
    var context = request.Properties["MS_HttpContext"] as HttpContextWrapper;
    if (context != null)
    {
        return HttpContextBaseExtensions.GetOwinContext(context.Request);
    }
    return null;
}
用法:

public ApplicationUserManager UserManager
{
   get
   {
      return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
   }
   private set
   {
      _userManager = value;
   }
}
public ApplicationUserManager用户管理器
{
得到
{
返回_userManager??Request.GetOwinContext().GetUserManager();
}
专用设备
{
_userManager=value;
}
}

这一行代码拯救了我的一天

       var manager = 
       new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var管理器=
新的ApplicationUserManager(新的UserStore(新的ApplicationDbContext());

您可以在控制器操作中使用它来获取UserManager的实例。

您使用任何DI框架吗?您的ApiController中不存在GetOwinContext()是什么意思?它是空的还是空的?对不起。该方法不存在查看我更新的答案。这是我过去常做的事情,但我在create函数中添加的选项不存在。使用:System.Web.HttpContext.Current.Request.GetOwinContext()是否更安全.GetUserManager…@Robertachman我不明白为什么这样更安全甚至更好,因为
HttpContext.Current
是一个静态属性,返回线程的当前
HttpContext
。这个答案实际上没有回答这个问题,因为在ApicController中无法访问
HttpContext
。使用
System.Web.Http.Owin.dll
中的
System.Net.Http
扩展可以从nuget插件中很好地工作-这是更好的答案,应该是可以接受的答案。不依赖HttpContext.Current。我完全同意,
HttpContext.Current
引入了对
System.Web
的依赖,这在自托管场景中不可用<代码>请求。注入GetOwinContext(),因此允许测试功能。后者都不是。顺便说一句
Request.GetOwinContext()
是在Microsoft.AspNet.WebApi.Owin nuget Package中定义的。这有助于我避免在使用
HttpContext.Current.GetOwinContext()
时引发的异常。这要好得多。同意这是一个更好的答案,因为它避免了围绕
HttpContext.Current
的潜在异常问题。然后您就可以编写
Request?.GetOwinContext()
public ApplicationUserManager UserManager
{
   get
   {
      return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
   }
   private set
   {
      _userManager = value;
   }
}
       var manager = 
       new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));