The view contains two tables: one for users who are not members of the selected role and one for those who are members. Each user’s name is displayed along with a check box that allows the membership to be changed.
该视图含有两个表格:一个用于不是所选角色成员的用户,一个是所选角色成员的用户。每个被显示出来的用户名称旁边都有一个复选框,可以修改其成员情况。
14.3.6 测试角色成员的编辑
Adding the AppRoleManager class to the application causes the Entity Framework to delete the contents of the database and rebuild the schema, which means that any users you created in the previous chapter have been removed. So that there are users to assign to roles, start the application and navigate to the /Admin/Index URL and create users with the details in Table 14-10.
在应用程序中添加AppRoleManager类会导致Entity Framework删除数据库的内容,并重建数据库架构,这意味着在上一章创建的用户都会被删除。因此,为了有用户可以赋予角色,需启动应用程序并导航到/Admin/Index URL,先创建一些如表14-10所示的用户。
表14-10. 创建示例用户的值
Name
用户名
Email
E-mail
Password
口令
Alice alice@example.com MySecret
Bob bob@example.com MySecret
Joe joe@example.com MySecret
Tip deleting the user database is fine for an example application but tends to be a problem in real applications. I show you how to gracefully manage changes to the database schema in Chapter 15.
提示:删除用户对示例应用程序而言没什么问题,但对实际应用程序来说就是一个问题了。第15章将演示如何优雅地修改数据库架构。
To test managing role memberships, navigate to the /RoleAdmin/Index URL and create a role called Users, following the instructions from the “Testing, Creating, and Deleting Roles” section. Click the Edit button and check the boxes so that Alice and Joe are members of the role but Bob is not, as shown in Figure 14-5.
为了测试角色成员的管理,导航到/RoleAdmin/Index URL,并按照“测试角色的创建和删除”小节的说明,创建一个名称为Users的角色。点击“Edit”按钮,并选中复选框,使Alice和Joe成为该角色的成员,而Bob别选,如图14-5所示。
Figure 14-5. Editing role membership
图14-5. 编辑角色成员
Tip If you get an error that tells you there is already an open a data reader, then you didn’t set the MultipleActiveResultSets setting to true in the connection string in Chapter 13.
提示:如果出现错误,告诉你说,已经有一个打开的数据读取程序,那是因为你并未将第13章连接字符串中的MultipleActiveResultSets设置为true。
Click the Save button, and the controller will update the role memberships and redirect the browser to the Index action. The summary of the Users role will show that Alice and Joe are now members, as illustrated by Figure 14-6.
点击“Save”按钮,于是控制器将更新角色成员,并将浏览器重定向到Index动作。Users角色的摘要将显示Alice和Joe现在已经是成员,如图14-6所示。
Figure 14-6. The effect of adding users to a role
图14-6. 将用户添加到角色的效果
14.3.7 使用角色进行授权
Now that I have the ability to manage roles, I can use them as the basis for authorization through the Authorize attribute. To make it easier to test role-based authorization, I have added a Logout method to the Account controller, as shown in Listing 14-16, which will make it easier to log out and log in again as a different user to see the effect of role membership.
现在已经能够管理角色了,通过Authorize注解属性,还可以将角色作为授权的基础。为了更易于测试基于角色的授权,我在Account控制器中添加了一个Logout方法,如清单14-16所示,这样便很容易注销,也容易作为不同用户登录,以看看角色成员的效果。
Listing 14-16. Adding a Logout Method to the AccountController.cs File
清单14-16. 在AccountController.cs文件中添加Logout方法
namespace Users.Controllers {
[Authorize] public class AccountController : Controller {
[AllowAnonymous] public ActionResult Login(string returnUrl) { ViewBag.returnUrl = returnUrl; return View(); }
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginModel details, string returnUrl) { // ...statements omitted for brevity... // ...出于简化,忽略了一些语句... }
[Authorize] public ActionResult Logout() { AuthManager.SignOut(); return RedirectToAction("Index", "Home"); }
private IAuthenticationManager AuthManager { get { return HttpContext.GetOwinContext().Authentication; } }
private AppUserManager UserManager { get { return HttpContext.GetOwinContext().GetUserManager<AppUserManager>(); } } } }