2014-10-13 3 views
13

Ich habe ASP.NET Identity 2 zum Erstellen der Rolle verwendet, aber das Ergebnis HttpContext.GetOwinContext().GetUserManager<AppRoleManager>() war null.HttpContext.GetOwinContext(). GetUserManager <AppRoleManager>() gibt null zurück

Dann konnte ich die Rolle nicht erstellen.

Wie kann ich dieses Problem lösen?

public class MVCController : Controller 
{ 
    public MVCController() 
    { 

    } 
    public AppRoleManager RoleManager // returns null ?! 
    { 
     get 
     { 
      return HttpContext.GetOwinContext().GetUserManager<AppRoleManager>(); 
     } 
    } 
    public User CurrentUser 
    { 
     get 
     { 
      string currentUserId = User.Identity.GetUserId(); 
      User currentUser = DataContextFactory.GetDataContext().Users.FirstOrDefault(x => x.Id.ToString() == currentUserId); 
      return currentUser; 
     } 
    } 
    public IAuthenticationManager AuthManager 
    { 
     get 
     { 

      return HttpContext.GetOwinContext().Authentication; 
     } 
    } 
    public AppUserManager UserManager 
    { 
     get 
     { 
      return HttpContext.GetOwinContext().GetUserManager<AppUserManager>(); 
     } 
    } 
} 

Mein Controller:

public class RoleAdminController : MVCController 
{ 
    [HttpPost] 
    public async Task<ActionResult> CreateRole([Required]string name) 
    { 
     if (ModelState.IsValid) 
     { 
      if (RoleManager != null) // RoleManager is null, why?! 
      { 
       IdentityResult result = await RoleManager.CreateAsync(new Role { Name = name }); 
       if (result.Succeeded) 
       { 
        return RedirectToAction("Index"); 
       } 
       else 
       { 
        AddErrorsFromResult(result); 
       } 
      } 
     } 
     return View(name); 
    } 
} 

AppRoleManager:

public class AppRoleManager : RoleManager<Role, int>, IDisposable 
{ 
    public AppRoleManager(RoleStore<Role, int, UserRole> store) 
     : base(store) 
    { 
    } 
    public static AppRoleManager Create(IdentityFactoryOptions<AppRoleManager> options, IOwinContext context) 
    { 
     return new AppRoleManager(new RoleStore<Role, int, UserRole>(DataContextFactory.GetDataContext())); 
    } 
} 
+1

Probieren Sie 'HttpContext.GetOwinContext(). Get ();' – trailmax

+2

@trailmax: Gibt null zurück. – Jahan

+0

Was ist der Unterschied zwischen Get und GetUserManager? – mjohnsonengr

Antwort

26

Wahrscheinlich haben Sie verpasst haben, den Weg zu geben OwinContext zu ApplicationUserManager erstellen.
Für, dass Sie diese

public void Configuration(IAppBuilder app) in Ihren haben brauchen
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
app.CreatePerOwinContext<AppRoleManager>(AppRoleManager.Create); 

Dies wird die Delegierten registrieren, die Sie danach UserManager und RoleManager mit OwinContext und nur erstellen können diese in Ihren Controller zurückrufen.

+0

erste Zeile wird bereits hinzugefügt, aber als ich die zweite Zeile Ihres Codes hinzugefügt habe, habe ich eine Fehlermeldung .... Compiler Fehlermeldung: CS1928: 'Owin.IAppBuilder' enthält keine Definition für 'CreatePerOwinContext' und die beste Erweiterungsmethode Überladung 'Owin.AppBuilderExtensions.CreatePerOwinContext (Owin.IAppBuilder, System.Func )' hat einige ungültige Argumente – Jahan

+1

@Jahan appologies, korrigierte meine Antwort, es sollte 'AppRoleManager' als generischer Parameter in der zweiten Zeile sein. – trailmax

+2

Vielen Dank für Ihre Antwort. – Jahan