本系列的的角色权限管理主要采用Dotnet MVC4工程内置的权限管理模块Simplemembership实现,主要有关文件是InitializeSimpleMembershipAttribute.cs和AccountModels.cs

下面是对这两个文件的了解和改造

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);表示数据库表不存在的情况下,通过连接到包含用户信息的数据库以及使用指定的成员资格或角色提供程序来初始化成员资格系统。

首先创建一个MVCSystem的空数据库,如下:

然后改造配置文件Web.config数据库链接,注意这里的name="DefaultConnection"与上面的WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);相同

然后运行程序,在空数据库MVCSystem中生成以下数据表(有的可能无法生存webpages_Permission,webpages_PermissionsInRoles表,不过没关系,手动建(字段参考下图)就可以了)

对于运行后无法自动生成数据表的同学,可以点击一下Home page上的注册按钮!

接下来创建和改造Models类:

AccountModels.cs【管理员管理页面的一些基本的ViewModel】

 using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Globalization;
using System.Web.Security; namespace MVCSystem.Web.Models
{ public class RegisterExternalLoginModel
{
[Required]
[Display(Name = "用户名")]
public string UserName { get; set; } public string ExternalLoginData { get; set; }
} public class LocalPasswordModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "当前密码")]
public string OldPassword { get; set; } [Required]
[StringLength(, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = )]
[DataType(DataType.Password)]
[Display(Name = "新密码")]
public string NewPassword { get; set; } [DataType(DataType.Password)]
[Display(Name = "确认新密码")]
[Compare("NewPassword", ErrorMessage = "新密码和确认密码不匹配。")]
public string ConfirmPassword { get; set; }
} public class LoginModel
{
[Required]
[Display(Name = "用户名")]
public string UserName { get; set; } [Required]
[DataType(DataType.Password)]
[Display(Name = "密码")]
public string Password { get; set; } [Display(Name = "记住我?")]
public bool RememberMe { get; set; }
} public class RegisterModel
{
[Required]
[Display(Name = "用户名")]
public string UserName { get; set; } [Required]
[StringLength(, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = )]
[DataType(DataType.Password)]
[Display(Name = "密码")]
public string Password { get; set; } [DataType(DataType.Password)]
[Display(Name = "确认密码")]
[Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
public string ConfirmPassword { get; set; }
} public class ExternalLogin
{
public string Provider { get; set; }
public string ProviderDisplayName { get; set; }
public string ProviderUserId { get; set; }
}
}

M_UserProfile.cs【我们可以扩展管理员数据表字段,电话,地址,邮箱等】

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web; namespace MVCSystem.Web.Models
{ [Table("UserProfile")]
public class M_UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[Display(Name = "ID")]
public int UserId { get; set; } [Display(Name = "用户名")]
[StringLength()]
public string UserName { get; set; } [Display(Name = "邮件地址")]
[StringLength()]
public string Email { get; set; }
}
}

M_Membership.cs【这个主要存储管理员的信息内容,以后要用到(比如重置密码)】

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web; namespace MVCSystem.Web.Models
{ [Table("webpages_Membership")]
public class M_Membership
{
public M_Membership()
{
Roles = new List<M_Roles>();
} [Key]
public int UserId { get; set; }
public DateTime? CreateDate { get; set; }
public string ConfirmationToken { get; set; }
public bool? IsConfirmed { get; set; }
public DateTime? LastPasswordFailureDate { get; set; }
public int PasswordFailuresSinceLastSuccess { get; set; }
public string Password { get; set; }
public DateTime? PasswordChangedDate { get; set; }
public string PasswordSalt { get; set; }
public string PasswordVerificationToken { get; set; }
public DateTime? PasswordVerificationTokenExpirationDate { get; set; }
public ICollection<M_Roles> Roles { get; set; }
}
}

M_Permission.cs【用来存储具体的Action权限】

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web; namespace MVCSystem.Web.Models
{ [Table("webpages_Permission")]
public class M_Permission
{
public M_Permission()
{
T_PermissionsInRoles = new List<M_PermissionsInRoles>();
} [Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[Display(Name = "权限ID")]
public int PermissionId { get; set; } [Display(Name = "名称")]
public string PermissionName { get; set; } [ForeignKey("PermissionId")]
public ICollection<M_PermissionsInRoles> T_PermissionsInRoles { set; get; }
}
}

M_Roles.cs【角色表,与管理员角色表、角色权限表关联】

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web; namespace MVCSystem.Web.Models
{ [Table("webpages_Roles")]
public class M_Roles
{ public M_Roles()
{
T_Members = new List<M_Membership>();
T_PermissionsInRoles = new List<M_PermissionsInRoles>();
} [Key]
[Display(Name = "ID")]
public int RoleId { get; set; } [Display(Name = "名称")]
[StringLength()]
public string RoleName { get; set; } public ICollection<M_Membership> T_Members { get; set; } [ForeignKey("RoleId")]
public ICollection<M_PermissionsInRoles> T_PermissionsInRoles { set; get; } }
}

M_UsersInRoles.cs【用来存储用户的角色】

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web; namespace MVCSystem.Web.Models
{ [Table("webpages_UsersInRoles")]
public class M_UsersInRoles
{
[Key]
public int UsersInRolesId { get; set; }
public int UserId { get; set; }
public int RoleId { get; set; }
}
}

M_PermissionsInRoles.cs【用来存储角色的具体权限】

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web; namespace MVCSystem.Web.Models
{ [Table("webpages_PermissionsInRoles")]
public class M_PermissionsInRoles
{
[Key]
public int Id { get; set; } public int RoleId { get; set; }
public int PermissionId { get; set; } public M_Roles T_Role { get; set; } public M_Permission T_Permission { get; set; }
}
}

然后创建一个Common文件夹,主要存储一些公告类:

简述:EF5.0包括Code First和DbContext API。DbContext API为EF提供更多的工作方式:Code First,Database First和Model First。

使用DbContext构造函数

1. Code First约定连接

namespace Magic.Unicorn
{
    public class UnicornsContext : DbContext
    {
        public UnicornsContext()
        // C# will call base class parameterless constructor by default
        {
        }
    }
}

用Magic.Unicorn.UnicornsContext作为数据库名,在本机上生成该数据库的连接字符串(SQL Express)。

2. Code First指定数据库名称的约定连接

public class UnicornsContext : DbContext
{
    public UnicornsContext()
        : base("UnicornsDatabase")
    {
    }
}

MVCStystemContext.cs实体类集合

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using MVCSystem.Web.Models; namespace MVCSystem.Web.Common
{
public class MVCStystemContext : DbContext
{
public MVCStystemContext()
: base("DefaultConnection")
{
} public DbSet<M_UserProfile> DB_UserProfiles { get; set; }
public DbSet<M_Membership> DB_Membership { get; set; }
public DbSet<M_Roles> DB_Roles { get; set; }
public DbSet<M_UsersInRoles> DB_UsersInRoles { get; set; }
}
}

源码下载:http://www.yealuo.com/Sccnn/Detail?KeyValue=2f926407-f80b-4bff-a729-949a53efed7b

作者:boyzi007
出处:http://www.cnblogs.com/boyzi/

QQ:470797533

QQ交流群:364307742
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(1)的更多相关文章

  1. ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(3)

    接下来完成用户.角色的增删查改,以及用户角色.权限的设置 对用户表.角色表做了一些扩展如下[可以更加自己需要增减字段] 相应的M_UserProfile.cs.M_Roles.cs进行扩展 using ...

  2. ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(2)

    创建公共分页参数类Common/GridPager.cs using System; using System.Collections.Generic; using System.Linq; usin ...

  3. ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(4)

    接下来就是菜单管理了,菜单分为两部分,一部分是菜单管理,另一部分是左边的树形菜单 数据库添加菜单表Menus USE [MVCSystem] GO /****** Object: Table [dbo ...

  4. ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(开篇)

    系统预览: 源码下载:http://www.yealuo.com/Home/Detail?KeyValue=2f926407-f80b-4bff-a729-949a53efed7b 创建项目,新手按步 ...

  5. ASP.NET MVC4.0+EF+LINQ+bui+网站+角色权限管理系统(6)

    快过年了,公司事情忙,好几天没有继续写博客,今天开始写账户模块系统登录,账户管理以及登录日志, 首先新建登录日志数据表: USE [MVCSystem] GO /****** Object: Tabl ...

  6. ASP.NET MVC4.0+EF+LINQ+bui+网站+角色权限管理系统(7)

    今天将使用Simplemembership进行权限控制 我们使用mvc的AuthorizeAttribute来实现对Controller and Action权限控制 看如下标为红色的代码片段: // ...

  7. ASP.NET MVC4.0+EF+LINQ+bui+网站+角色权限管理系统(5)

    我参考了bui官网,里面提供了大量的接口案例和效果,之前下载的前端框架完全不需要bootstrap,所以从这一节开始,不再使用bootstrap(当然不想改变的也可以继续使用之前的框架,不影响使用), ...

  8. ASP.NET MVC4.0+ WebAPI+EasyUI+KnockOutJS快速开发框架 通用权限管理系统

    1.基于 ASP.NET MVC4.0 + WebAPI + EasyUI + Knockout 的架构设计开发 2.采用MVC的框架模式,具有耦合性低.重用性高.生命周期成本低.可维护性高.有利软件 ...

  9. 翻译:使用 ASP.NET MVC 4, EF, Knockoutjs and Bootstrap 设计和开发站点 - 3

    原文地址:http://ddmvc4.codeplex.com/ 原文名称:Design and Develop a website using ASP.NET MVC 4, EF, Knockout ...

随机推荐

  1. oracle控制何时触发审计动作

    1)By session / By Access by session对每个session中发生的重复操作只记录一次 by access对每个session中发生的每次操作都记录,而不管是否重复. 对 ...

  2. hdu5441 并查集 长春网赛

    对于每次询问的大的值,都是从小的值开始的,那就从小到大处理,省去很多时间,并且秩序遍历一遍m; 这题cin容易超时,scanf明显快很多很多.G++又比C++快; //这代码scanf400+,cin ...

  3. Run As none applicable

    详解如何在myeclipse中运行JSP,Run As none applicable(图) 内容提要:对JSP的访问都是用浏览器进行的,没有Run on Server这个选项.   在MyEclip ...

  4. HTML5入门指南

    1.HTML5到底是什么? HTML5是HTML最新的修订版本,2014年10月由万维网联盟(W3C)完成标准制定.目标是取代1999年所制定的HTML 4.01和XHTML 1.0标准,以期能在互联 ...

  5. 终端安装opencv

    安装 要想在 notebook 中安装和使用 OpenCV,请打开终端窗口(也被称为 Windows 用户的命令提示符窗口),并使用以下命令通过 conda 安装最新版本 (v3): conda in ...

  6. es6 promise简析

    1.Promise的含义 所谓Promise,就是一个对象,用来传递异步操作的消息. Promise对象有以下两个特点: 对象的状态不受外界影响.Promise对象代表一个异步操作,有三种状态:Pen ...

  7. url地址栏参数<==>对象(将对象转换成地址栏的参数以及将地址栏的参数转换为对象)的实用函数

    /** * @author web得胜 * @param {Object} obj 需要拼接的参数对象 * @return {String} * */ function obj2qs(obj) { i ...

  8. Project Euler Problem 26-Reciprocal cycles

    看样子,51nod 1035 最长的循环节 这道题应该是从pe搬过去的. 详解见论文的(二)那部分:http://web.math.sinica.edu.tw/math_media/d253/2531 ...

  9. 最新版本的ADT使用问题

    昨天把androidsdk和adt更新到最新版本,android sdk r22版本. 更新完后原来的项目打包后出现第三方JAR包找不到,网上搜了半天终于找到问题所在: 新版本多了一个Android ...

  10. 在phpstudy中nginx伪静态配置

    ########################### #以下是虚拟主机配置 server { listen 80; server_name hzym.com; root "D:\phpst ...