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 ...
随机推荐
- Nginx - 01 - Nginx初体验
首先下载Nginx,这里电脑太垃圾,没法装虚拟机,所以只能用Windons版本的Nginx. 安装包下载地址:http://nginx.org/en/download.html 下载下来,然后解压: ...
- Directx11 教程(1) 基本的windows应用程序框架(1)
原文:Directx11 教程(1) 基本的windows应用程序框架(1) 在vs2010中,建立一个新的win32工程,名字是: myTutorialD3D11, 注意:同时勾选Cr ...
- 模拟登录新浪微博(Python) - 转
Update: 如果只是写个小爬虫,访问需要登录的页面,采用填入cookie 的方法吧,简单粗暴有效,详细见:http://www.douban.com/note/264976536/模拟登陆有时需要 ...
- time,datetime模块
time模块 时间戳 返回1970年1月1日 00:00:00开始按秒计算时间偏移量 time_stamp = time.time() print(time_stamp,type(time_stamp ...
- 用预编译包安装zabbix-agent
如果主机无法上网,安装rpm又缺少依赖时,可以通过预编译包进行安装zabbix-agent,下载地址 https://www.zabbix.com/download 下载后,执行如下命令: wget ...
- hdu2041 dp
#include<stdio.h> int main() { int i,t,n; ]; dp[]=; dp[]=; dp[]=; ;i<=;i++) dp[i]=dp[i-]+dp ...
- sn图书spider
# -*- coding: utf-8 -*-import scrapyfrom copy import deepcopy class SnbookSpider(scrapy.Spider): nam ...
- python 类与类之间的关系. 特殊成员
一.类与类之间的关系 1.依赖关系 在方法的参数位置把另一个类的对象作为参数进行传递 class Person: def play(self, tools): # 通过参数的传递把另一个类的对象传递进 ...
- 你真的知道你看到的UTF-8字符是什么吗?
翻译自http://www.pixelstech.net/article/1397877200-You-know-what-UTF-8-is-when-you-see-it- Source : son ...
- Data Flow-File Read-详细过程