ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(1)
本系列的的角色权限管理主要采用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)的更多相关文章
- ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(3)
接下来完成用户.角色的增删查改,以及用户角色.权限的设置 对用户表.角色表做了一些扩展如下[可以更加自己需要增减字段] 相应的M_UserProfile.cs.M_Roles.cs进行扩展 using ...
- ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(2)
创建公共分页参数类Common/GridPager.cs using System; using System.Collections.Generic; using System.Linq; usin ...
- ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(4)
接下来就是菜单管理了,菜单分为两部分,一部分是菜单管理,另一部分是左边的树形菜单 数据库添加菜单表Menus USE [MVCSystem] GO /****** Object: Table [dbo ...
- ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(开篇)
系统预览: 源码下载:http://www.yealuo.com/Home/Detail?KeyValue=2f926407-f80b-4bff-a729-949a53efed7b 创建项目,新手按步 ...
- ASP.NET MVC4.0+EF+LINQ+bui+网站+角色权限管理系统(6)
快过年了,公司事情忙,好几天没有继续写博客,今天开始写账户模块系统登录,账户管理以及登录日志, 首先新建登录日志数据表: USE [MVCSystem] GO /****** Object: Tabl ...
- ASP.NET MVC4.0+EF+LINQ+bui+网站+角色权限管理系统(7)
今天将使用Simplemembership进行权限控制 我们使用mvc的AuthorizeAttribute来实现对Controller and Action权限控制 看如下标为红色的代码片段: // ...
- ASP.NET MVC4.0+EF+LINQ+bui+网站+角色权限管理系统(5)
我参考了bui官网,里面提供了大量的接口案例和效果,之前下载的前端框架完全不需要bootstrap,所以从这一节开始,不再使用bootstrap(当然不想改变的也可以继续使用之前的框架,不影响使用), ...
- ASP.NET MVC4.0+ WebAPI+EasyUI+KnockOutJS快速开发框架 通用权限管理系统
1.基于 ASP.NET MVC4.0 + WebAPI + EasyUI + Knockout 的架构设计开发 2.采用MVC的框架模式,具有耦合性低.重用性高.生命周期成本低.可维护性高.有利软件 ...
- 翻译:使用 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 ...
随机推荐
- Spring → 04:Bean(1)
一.Bean概念 Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象. Spring 容器会自动完成@bean对象的实例化. 创建应用对象之间的协作关系的行为称为:装配( ...
- linux的简单操作
查看当前用户who am i 创建用户:sudo adduser lilei然后输入密码 查看用户:ls /home 用新用户登陆:su -l lilei 查看所属用户组:groups 用户名 新建文 ...
- zabbix源码编译安装以及添加第一台host监控
基础准备 硬件需求 数据库需求 软件需求 其他软件需求 安装 安装方式 source code 编译好的二进制包 rpm或者deb 源码编译安装部署zabbix以及附件 前提准备 最小化安装操作系 ...
- QT_OPENGL-------- 1. WINDOW
opengl学习第一步,首先来实现一个显示窗口. 1.首先要下载配置glfw,我在前面的文章中也提到过,具体作用可以另行百度.配置出现无法引用可参考ubuntu 使用glfw.h 出现函数无法调用. ...
- spider csdn博客和quantstart文章
spider csdn博客和quantstart文章 功能 提取csdn博客文章 提取quantstart.com 博客文章, Micheal Hall-Moore 创办的网站 特色功能就是: 想把原 ...
- HZOJ 简单的期望
性质:一个数分解质因数后2的次数=二进制下末尾连续0的个数. 乘2比较好考虑,比较恶心的是+1.一个$k*2^0$的数+1后可能会出现很多情况.但是k这个数表示不出来. 但是加的操作最多有200次,也 ...
- ArcGIS 发布高程服务。10.4
ArcGIS 发布高程必须是10.21以上,我用10.4. 前端用ArcGIS For API 4.x. ARCGIS很早之前有CS版本的ArcScene,可查看高程TIF文件,但机制和BS的完全不同 ...
- hdu 2196【树形dp】
http://acm.hdu.edu.cn/showproblem.php?pid=2196 题意:找出树中每个节点到其它点的最远距离. 题解: 首先这是一棵树,对于节点v来说,它到达其它点的最远距离 ...
- Python基础:07迭代器
迭代器是在版本 2.2 被加入Python 的,它为类序列对象提供了一个类序列的接口.Python 的迭代无缝地支持序列对象,而且它还允许迭代非序列类型,包括用户定义的对象.它的出现,对列表迭代.字典 ...
- ModuleNotFoundError: No module named 'tools.nnwrap' pytorch 安装
https://pytorch.org/get-started/locally/ pytorch 主页选择后安装