Domanda

Sto seguendo un campione di Microsoft per implementare la convalida e-mail con Identity 2.0.0

Sono bloccato in questa parte

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

funziona in un controller ma HttpContext non contiene alcun metodo GetOwinContext in un Apicontroller .

Così ho provato HttpContext.Current.GetOwinContext() ma il metodo GetUserManager non esiste.

Non riesco a capire un modo per ottenere il UserManager che ho costruito 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); 
    ...
}
.

Questa riga

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

Chiama la seguente funzione per configurare 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;
}
.

Come posso accedere a questo UserManager in un ApiController?

È stato utile?

Soluzione

Ho davvero frainteso la tua domanda prima.Ti stai solo mancando alcune affermazioni, penso.

Il GetOwinContext().GetUserManager<ApplicationUserManager>() è in Microsoft.AspNet.Identity.Owin.

Quindi prova ad aggiungere questa parte:

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

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

Altri suggerimenti

Questo metodo di estensione potrebbe essere una soluzione migliore se si desidera controllare l'unità dei controller.

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;
}
.

Uso:

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

Questa singola riga di codice ha salvato il mio giorno ...

       var manager = 
       new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));
.

Puoi usarlo all'interno di un'azione del controller per ottenere un'istanza di usermanager.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top