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

问题描述

我正在按照 Microsoft 示例使用 Identity 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;
   }
}

这在 controller 中有效,但 HttpContextApiController 中不包含任何 GetOwinContext 方法.

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.

我想不出一种方法来获取我在 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;
}

如何在 ApiController 中访问这个 UserManager?

How can I access this UserManager in an ApiController?

推荐答案

我之前真的误解了你的问题.我想你只是缺少一些 using 语句.

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

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>>();

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

07-31 10:15