2016-04-07 18 views
0

Ich bin mit Rollen in meinem asp netvvm 5 Projekt stecken. Ich kann Rollentyp auf Datenbank erstellen, aber kann sie nicht in Benutzerregistrierung abrufen, da es einen Fehler auf meiner Register.cshtml "There is kein ViewData-Element vom Typ '' mit dem Schlüssel 'Name'. Kann mir bitte jemand helfen. danke im voraus. Hier ist mein Controller:Asp Net MVC Rollensicherheit

private ApplicationSignInManager _signInManager; 
      private ApplicationUserManager _userManager; 
      ApplicationDbContext context; 
      public AccountController() 
      {  

      } 

      public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager) 
      { 
       UserManager = userManager; 
       SignInManager = signInManager; 
       context = new ApplicationDbContext(); 
      } 

Meine Register Aktion

[AllowAnonymous] 
     public ActionResult Register() 
     { 
      return View(); 
     } 

     // 
     // POST: /Account/Register 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Register(RegisterViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; 
       ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name"); 
       var result = await UserManager.CreateAsync(user, model.Password); 
       if (result.Succeeded) 
       { 
        await this.UserManager.AddToRoleAsync(user.Id, model.Name); 
        await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 
        return RedirectToAction("Index", "Home"); 
       } 
       AddErrors(result); 
      } 
      // If we got this far, something failed, redisplay form 

      return View(model); 
     } 

My View

<div class="form-group"> 
      @Html.Label("Select Your User Type", new { @class = "col-md-2 control-label" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("Name") 

      </div> 

Antwort

0

obwohl ich eine andere Art und Weise geschrieben haben uns die Rollen aber Ihren Code zu finden fein haben Sie betreten Alle Rollen in der Tabelle

AspNetRoles

und erstellt die Beziehung zwischen Benutzer und Rollen in Tabelle

AspNetUserRoles

Um alle die Rolle für bestimmte Benutzer finden Sie den folgenden Code

RoleBasedSecurity.Models.ApplicationDbContext context = new ApplicationDbContext(); 
    ApplicationUser au = context.Users.First(u => u.UserName == "[email protected]"); 

    foreach (IdentityUserRole role in au.Roles) 
    { 
     string name = role.RoleId; 
     string RoleName = context.Roles.First(r => r.Id == role.RoleId).Name; 
    } 
verwenden können

Code zum Erstellen von Rollen Schreiben Sie diese Zeilen in protected override void Seed (RoleBasedSecurity.Models.ApplicationDbContext Kontext) in cofiguration.cs

if (!context.Roles.Any(r => r.Name == "User")) 
    { 
     var store = new RoleStore<IdentityRole>(context); 
     var manager = new RoleManager<IdentityRole>(store); 
     var role = new IdentityRole { Name = "User" }; 

     manager.Create(role); 
    } 

-Code für Benutzer Rolle schreiben diese Zeilen in protected override void Seed (RoleBasedSecurity.Models.ApplicationDbContext Kontext) Anbringen in cofiguration.cs

if (!context.Users.Any(u => u.UserName == "[email protected]")) 
    { 
     var store = new UserStore<ApplicationUser>(context); 
     var manager = new UserManager<ApplicationUser>(store); 
     var user = new ApplicationUser { UserName = "[email protected]", Email = "[email protected]" }; 
     manager.Create(user, "password"); 
     manager.AddToRole(user.Id, "Admin"); 
    } 

die Rollen für bestimmte Benutzer zu bringen, müssen Sie diesen Code schreiben.

ApplicationUser user = UserManager.FindByName(model.UserName); 
string userId = user != null ? user.Id : null; 
var roles = userId != null ? UserManager.GetRoles(userId) : null; 
if (roles != null) 
{ 
    foreach (var item in roles) 
    { 
     //Assign user roles 
     UserManager.AddToRole(userId, item); 
    } 
} 

und alle Rollen tun dies

ApplicationDbContext context = new ApplicationDbContext(); 
    var roles = context.Roles; 
+0

Vielen Dank für Ihre Antwort zu bekommen. Ja, ich habe es geschafft, Rollen auf ASpNetRoles einzufügen, aber ich kann sie nicht zu meinen AspNetUsers bringen, da dies einen Fehler verursacht, wie oben auf diesem Beitrag angegeben. – prezequias

+0

Überprüfen Sie das aktualisierte Ergebnis – rashfmnb