无法从OwinContext在apicontroller得到的UserManager [英] Can't get UserManager from OwinContext in apicontroller

查看:1265
本文介绍了无法从OwinContext在apicontroller得到的UserManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的微软样品来实现电子邮件验证与身份2.0.0

I'm following a Microsoft sample to implement email validation with Identity 2.0.0

我被困在这部分

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

这个工程在控制器,但的HttpContext 不包含任何 GetOwinContext 在方法中的 ApiController

This works in an controller but HttpContext doesn't contain any GetOwinContext method in an ApiController.

所以,我想 HttpContext.Current.GetOwinContext()但该方法 GetUserManager 不存在。

So I tried HttpContext.Current.GetOwinContext() but the method GetUserManager doesn't exist.

我不能想出一个办法来获得的UserManager 我打造的 Startup.Auth.cs

I can't figure out a way to get the UserManager I build in Startup.Auth.cs

// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
    //Configure the db context, user manager and role manager to use a single instance per request
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
    ...
}

这行

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

调用下面的功能配置的UserManager

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
     var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
     //Configure validation logic for usernames
     manager.UserValidator = new UserValidator<ApplicationUser>(manager)
     {
         AllowOnlyAlphanumericUserNames = false,
         RequireUniqueEmail = true
     };

     // Configure user lockout defaults
     manager.UserLockoutEnabledByDefault = true;
     manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
     manager.MaxFailedAccessAttemptsBeforeLockout = 5;

     manager.EmailService = new EmailService();

     var dataProtectionProvider = options.DataProtectionProvider;
     if (dataProtectionProvider != null)
     {
         manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
     }
     return manager;
}

我如何可以访问此的UserManager ApiController

推荐答案

我真的误会了你的问题早。你只是缺少一些using语句,我想。

I really misunderstood your question earlier. You are just missing some using statements, I think.

GetOwinContext()GetUserManager&LT; ApplicationUserManager&GT;() Microsoft.AspNet.Identity.Owin

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

So try add this part:

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

var manager = HttpContext.Current.GetOwinContext().GetUserManager<UserManager<User>>();

这篇关于无法从OwinContext在apicontroller得到的UserManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆