UserManager kann nicht von OwinContext im Apicontroller abgerufen werden
-
20-12-2019 - |
Frage
Ich folge einem Microsoft-Beispiel zur Implementierung der E-Mail-Validierung mit Identity 2.0.0
Ich stecke bei diesem Teil fest
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
Dies funktioniert in einem controller
Aber HttpContext
enthält keine GetOwinContext
Methode in einem ApiController.
Also habe ich es versucht HttpContext.Current.GetOwinContext()
sondern die Methode GetUserManager
existiert nicht.
Ich kann keinen Weg finden, das zu bekommen UserManager
Ich baue ein 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);
...
}
diese Linie
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
ruft die folgende Funktion auf, um die zu konfigurieren 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;
}
Wie kann ich darauf zugreifen? UserManager
in einem (n ApiController
?
Lösung
Ich habe deine Frage vorhin wirklich falsch verstanden.Ich denke, Ihnen fehlen einfach einige using-Anweisungen.
Der GetOwinContext().GetUserManager<ApplicationUserManager>()
ist in Microsoft.AspNet.Identity.Owin
.
Versuchen Sie also, diesen Teil hinzuzufügen:
using Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity; // Maybe this one too
var manager = HttpContext.Current.GetOwinContext().GetUserManager<UserManager<User>>();
Andere Tipps
Diese Erweiterungsmethode ist möglicherweise eine bessere Lösung, wenn Sie Ihre Controller einem Unit-Test unterziehen möchten.
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;
}
Verwendung:
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
Diese einzelne Codezeile hat meinen Tag gerettet ...
var manager =
new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));
Sie können es innerhalb einer Controller-Aktion verwenden, um eine Instanz von UserManager abzurufen.