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
}
希望這有助於:)
只記錄在人們可以從這個導航的最後一個項目,這是什麼意思? –
當你沒有登錄時,你會看到前兩個列表項,當你登錄時你會看到全部三個。 –