Asp.net web api 如何访问Web API控制器中的Identity UserManager以发送电子邮件

Asp.net web api 如何访问Web API控制器中的Identity UserManager以发送电子邮件,asp.net-web-api,asp.net-identity,Asp.net Web Api,Asp.net Identity,我正在使用Identity v2,需要使用中间件组件中的UserManager.SendAsync方法从Web Api控制器发送电子邮件。但是我不知道如何在webapiController方法中访问UserManager本身。我正在尝试一种类似于常规MVC控制器的方法,但usermanager总是null。有什么建议吗 public class MyApiController: ApiController { public MyApiController()

我正在使用
Identity v2
,需要使用中间件组件中的
UserManager.SendAsync
方法从
Web Api
控制器发送电子邮件。但是我不知道如何在
webapi
Controller方法中访问
UserManager
本身。我正在尝试一种类似于常规MVC控制器的方法,但usermanager总是
null
。有什么建议吗

  public class MyApiController: ApiController
    {

        public MyApiController()
        {

        }

        public MyApiController(ApplicationUserManager userManager)
        {
            UserManager = userManager;
        }

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

        public void SendEmail()
        {

           _userManager.SendAsync(...);
        }
    }
公共类MyApicController:ApicController
{
公共MyApiController()
{
}
公共MyApicController(ApplicationUserManager用户管理器)
{
UserManager=UserManager;
}
公共应用程序管理员用户管理器
{
得到
{
返回_userManager??HttpContext.Current.GetOwinContext().GetUserManager();
}
专用设备
{
_userManager=value;
}
}                 
public void sendmail()
{
_userManager.SendAsync(…);
}
}

当前解决方案将永远不会调用接受ApplicationUserManager的构造函数。将空构造函数更改为调用其他构造函数

public class MyApiController: ApiController
{

    public MyApiController()
    : this(new ApplicationUserManager())
    {

    }

 THE REST OF YOUR IMPLEMENTATION GOES HERE
}

检查是否已从启动类正确配置UserManager

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.DataProtection;
using Owin;

namespace Identity_PasswordPolicy
{
    public partial class Startup
    {
        // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
        public void ConfigureAuth(IAppBuilder app)
        {
            ///...

            // Configure the UserManager
            app.UseUserManagerFactory(new IdentityFactoryOptions<ApplicationUserManager>()
            {
                DataProtectionProvider = app.GetDataProtectionProvider(),
                Provider = new IdentityFactoryProvider<ApplicationUserManager>()
                {
                   OnCreate = ApplicationUserManager.Create
                }
            });

            /// ...
        }
    }
}
使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.EntityFramework;
使用Microsoft.AspNet.Identity.Owin;
使用Microsoft.Owin;
使用Microsoft.Owin.Security.Cookies;
使用Microsoft.Owin.Security.DataProtection;
使用Owin;
名称空间标识\密码策略
{
公共部分类启动
{
//有关配置身份验证的详细信息,请访问http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder应用程序)
{
///...
//配置用户管理器
app.UseUserManagerFactory(新标识FactoryOptions

Startup是分部类,从中调用方法ConfigureAuth by link


在示例中,它是从owin启动方法调用的,但根据hosing的不同,它可以从global.asax.cs调用。您是否使用任何DI?或者如何启动MyApicController?启动MyApicController是什么意思?我还定义了无参数构造函数。这是您问的吗?顺便问一下,我没有使用任何DI。更新您的任务ion然后使用空控制器…在ApiController中,我相信您会希望使用Request.GetOwinContext()并避免HttpContext.Current.new ApplicationUserManager需要一个IUserStore参数,我需要传入什么?new UserStore()UserManager现在可用,但我没有收到与我在常规MVC控制器中用于发送电子邮件的代码相同的电子邮件,并且工作正常。可能的原因是什么?我认为你可以比我更好地回答这个问题。也许你有一个实现IIdentialMessageServiceIdentityConfig.cs的服务,其中包含SendAsync u方法实现IIdentityMessageService的nder EmailService类。当我从Web Api调用它时,它不会被击中,但它是通过MVC控制器实现的。我发现,“UseUserManagerFactory”扩展方法不可用。我需要安装任何nuget软件包吗?您需要安装Microsoft.AspNet.Identity.Owin和Microsoft.AspNet.Identity.Core的nuget软件包。我在回答中向示例代码添加了usings。请检查它。实际上,此注释是正确的“在ApiController中,我相信您会希望使用Request.GetOwinContext()并避免使用HttpContext.Current”,这取决于宿主HttpContext.Current可能为空,因此请尝试检查它