2016-08-25 111 views
0

我是.net的新手,必須爲學校創建一個項目。我使用的是MVC 5模板,它有一個標準的登錄名,但現在我需要2個角色:學生和老師。我如何以及在哪裏創建這些角色?然後,我怎麼讓這個只有在登錄的人可以從這個導航.net MVC添加角色並添加用戶併爲用戶添加角色+使視圖的一部分僅對角色可見

<div class="navbar-collapse collapse"> 
      <ul class="nav navbar-nav"> 
       <li>@Html.ActionLink("Home", "Index", "Home")</li> 
       <li>@Html.ActionLink("Roles", "Index", "Roles")</li> 
       <li>@Html.ActionLink("Evaluaties", "About", "Home")</li> 
      </ul> 
      @Html.Partial("_LoginPartial") 
     </div> 

的最後一個項目加我想一個角色看到不同的看法與其他我該怎麼做呢?

+0

只記錄在人們可以從這個導航的最後一個項目,這是什麼意思? –

+0

當你沒有登錄時,你會看到前兩個列表項,當你登錄時你會看到全部三個。 –

回答

2

MVC5項目模板默認沒有角色管理器, 所以我們從創建角色管理器類開始; (爲了保持項目結構良好,最好是添加類,如下所述):

1-創建ApplicationRole類(添加到IdentityModels.cs模型文件夾下)

public class ApplicationRole : IdentityRole 
{ 
    public ApplicationRole() : base() { } 

    public ApplicationRole(string name) : base(name) { } 
} 

2-創建ApplicationRoleManager類(把它App_Start文件夾下的內部IdentityConfig.cs)

public class ApplicationRoleManager : RoleManager<ApplicationRole>, IDisposable 
{ 
    public ApplicationRoleManager(RoleStore<ApplicationRole> store) : base(store) { } 

    public static ApplicationRoleManager Create(
     IdentityFactoryOptions<ApplicationRoleManager> options, 
     IOwinContext context) 
    { 
     return new ApplicationRoleManager(new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>())); 
    } 
} 

3-配置的作用經理申請啓動;下面的行添加到ConfigureAuth(IAppBuilder應用程序)的方法中Startup.Auth.cs文件:

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

4-如果需要創建一個新的控制器或使用現有的,並定義ApplicationuserManager和ApplicationRoleManager內的參數控制器構造函數,然後從owin上下文檢索身份管理:

namespace UsersAndRoles.Controllers 
{ 
using Microsoft.AspNet.Identity.Owin; 
using System.Web; 
using System.Web.Mvc; 

    public class UsersAndRolesController : Controller 
    { 
     private ApplicationUserManager _userManager; 
     private ApplicationRoleManager _roleManager; 

     public UsersAndRolesController() { } 

     public UsersAndRolesController(ApplicationUserManager userManager, ApplicationRoleManager roleManager) 
     { 
      UserManager = userManager; 
      RoleManager = roleManager; 
     } 

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

     public ApplicationRoleManager RoleManager 
     { 
      get 
      { 
       return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>(); 
      } 
      private set 
      { 
       _roleManager = value; 
      } 
     } 

     // GET: UsersAndRoles 
     public ActionResult Index() 
     { 
      return View(); 
     } 
    } 
} 

現在的設置已經完成和控制器已準備好,以創建一個用戶只需創建一個ApplicationUser並添加創建用戶和角色, 它使用UserManager.Create方法,密碼必須匹配r在ApplicationUserManager類中定義的ules。

5-通過調用UserManager.Create方法來創建用戶:

var user = new ApplicationUser 
     { 
      UserName = "Ziyad", 
      Email = "[email protected]" 
     }; 

     var password = "[email protected]"; 
     UserManager.Create(user, password); 

6-創建以類似的方式角色和使用RoleManager:

var role = new ApplicationRole 
     { 
      Name = "Students" 
     }; 

     RoleManager.Create(role); 

7-最後一部分是向用戶分配角色使用的UserManager:

UserManager.AddToRole("user_id", "role_name"); 

完整的控制器是在這裏:

namespace UsersAndRoles.Controllers 
{ 
    using Microsoft.AspNet.Identity; 
    using Microsoft.AspNet.Identity.Owin; 
    using System.Web; 
    using System.Web.Mvc; 
    using Models; 
public class UsersAndRolesController : Controller 
{ 
    private ApplicationUserManager _userManager; 
    private ApplicationRoleManager _roleManager; 

    public UsersAndRolesController() { } 

    public UsersAndRolesController(ApplicationUserManager userManager, ApplicationRoleManager roleManager) 
    { 
     UserManager = userManager; 
     RoleManager = roleManager; 
    } 

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

    public ApplicationRoleManager RoleManager 
    { 
     get 
     { 
      return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>(); 
     } 
     private set 
     { 
      _roleManager = value; 
     } 
    } 

    public string CreateUser() 
    { 
     var user = new ApplicationUser 
     { 
      UserName = "Ziyad", 
      Email = "[email protected]" 
     }; 

     var password = "[email protected]"; 
     var result = UserManager.Create(user, password); 

     if (result.Succeeded) 
     { 
      return "User created"; 
     } 
     else 
     { 
      var msg = "Error, user not created"; 
      foreach (var err in result.Errors) 
       msg += err + "<br />"; 

      return msg; 
     }    
    } 

    public string CreateRole() 
    { 
     var role = new ApplicationRole 
     { 
      Name = "Teachers" 
     }; 

     var result = RoleManager.Create(role); 

     if (result.Succeeded) 
     { 
      return "Role created"; 
     } 
     else 
     { 
      var msg = "Error, role not created"; 
      foreach (var err in result.Errors) 
       msg += err + "<br />"; 

      return msg; 
     } 
    } 

    public string AddUserToRole() 
    { 
     var user = UserManager.FindByEmail("[email protected]"); 

     if (user != null) 
     { 
      var result = UserManager.AddToRole(user.Id, "Teachers"); 
      if (result.Succeeded) 
      { 
       return "User assigned to role"; 
      } 
      else 
      { 
       var msg = "Error, user not assigned to role <br />"; 
       foreach (var err in result.Errors) 
        msg += err + "<br />"; 

       return msg; 
      } 
     } 
     else 
     { 
      return "User not found!"; 
     } 
    } 
} 

}如果要限制某些觀點

/菜單特定角色使用的用戶。IsInRole( 「ROLE_NAME」)方法:

if (User.IsInRole("Teachers")) 
     { 
      // role specific options 
     } 
如果你想只允許特定角色訪問的操作方法使用授權屬性

[Authorize(Roles = "Teachers")] 
public ActionResult ActionName() 
{ 
    //teachers specific method 
} 

希望這有助於:)

0

您可以將角色保存到數據庫中,並且在用戶成功登錄後,您可以將角色添加到身份驗證Cookie中。請看我的回答here