2016-03-19 16 views
2

Ich versuche Cleanup-Jobs von meinem azurblauen Webjob für meine mvc-Anwendung auszuführen. Ich kann Standard-Datenbank-Updates kein Problem, aber ich bin nicht in der Lage, die Aspnetuser-Konten zu bereinigen, da ich einen ApplicationUser-Kontext als keine Startup-Klasse für Owin erhalten kann.Identität 2.0/Owin/ApplicationUser von azure Webjob

Wer hat irgendwelche Ideen, wie dies getan werden kann oder einige Dummy-Code? Meine Google-Suchen sind bis jetzt leer ausgegangen.

Danke.

Antwort

2

Ich bin mit ASP.net MVC 5 mit VS 2015

Eigentlich, auch wenn die ApplicationUserManager eine OwinContext erfordert, können Sie loswerden der OwinContext bekommen. Die OwinContext ist nur nützlich für den Zugriff auf die ApplicationDbConbtext, die an die OWinContext gebunden ist.

So Refactoring ich die Create Fabrikmethoden ApplicationUserManager, um den Anwender bei der Inbetriebnahme verwalten zu können (oder in einem WebJob), ohne OwinContext:

public static ApplicationUserManager Create(
        IdentityFactoryOptions<ApplicationUserManager> options, 
        IOwinContext context) 
{ 
    ApplicationDbContext dbContext = context.Get<ApplicationDbContext>(); 
    return Create(options, dbContext); 
} 

internal static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, Applicatio 
{ 
    // Create ApplicationUserManager myself instead of finding it on OwinContext 
    var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context)); 

    // old code already generated by VS wizard 
} 

Dann benutze ich die ApplicationUserManager:

using (ApplicationDbContext applicationDbContext = new ApplicationDbContext()) 
{ 
    var options = new IdentityFactoryOptions<ApplicationUserManager> 
    { 
     //DataProtectionProvider = app.GetDataProtectionProvider(), 
    }; 
    ApplicationUserManager userManager = ApplicationUserManager.Create(options, applicationDbContext); 
    // ApplicationRoleManager has been changed the same way to be usable without OwinContext 
    ApplicationRoleManager appRoleManager = ApplicationRoleManager.Create(applicationDbContext); 

    // ... Code using ApplicationUserManager and ApplicationRoleManager 
    ApplicationUser appUser = userManager.FindByEmailAsync(email).Result; 
    // ... 
    IdentityResult result = userManager.CreateAsync(appUser, password).Result; 

    } 
} 

Ich hoffe, es hilft

Verwandte Themen