本系列的的角色权限管理主要采用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. Apache Camel,Spring Boot 实现文件复制,转移 (转)

    基本框架 Apache Camel Spring Boot Maven 开发过程 1.新建一个POM(quickstart)项目,在POM文件中添加Camel和Spring Boot的依赖 <p ...

  2. Nginx教程(三) Nginx日志管理 (转)

    Nginx教程(三) Nginx日志管理 1 日志管理 1.1 Nginx日志描述 通过访问日志,你可以得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息:通过错误日志,你可以得到系统某 ...

  3. ubuntu 代理配置

    1.安装Python 2.安装shadowsocks客户端 sudo pip install shadowsocks 3.配置shadowsocks客户端配置 vim /etc/shadowsocks ...

  4. H5+ 分享到微信、朋友圈代码示例

    h5+分享到微信.朋友圈代码示例 在使用分享功能的时候会莫名的分享失败,debug时发现是图片过大的问题. 图片过大时ios平台上返回错误码-8,安卓上返回错误码-3(我测试是这样) 因此如果第一次分 ...

  5. Plupload的上传机制

    plupload支持多文件上传.经过测试发现,plupload在上传多个文件时,会把多个文件拆分成单个的一个一个上传.

  6. vue页面内监听路由变化

    beforeRouteEnter (to, from, next) { // 在渲染该组件的对应路由被 confirm 前调用 // 不!能!获取组件实例 `this` // 因为当钩子执行前,组件实 ...

  7. PHP判断图片格式的七种方法小结

    <?php $imgurl = "http://www.jb51.net/images/logo.gif"; //方法1 echo $ext = strrchr($imgur ...

  8. 【算法】BSGS算法

    BSGS算法 BSGS算法用于求解关于x的模方程\(A^x\equiv B\mod P\)(P为质数),相当于求模意义下的对数. 思想: 由费马小定理,\(A^{p-1}\equiv 1\mod P\ ...

  9. @codeforces - 1086F@ Forest Fires

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 一个无穷大的方格图,每个方格内都种了棵树. 一开始点燃了 n 棵 ...

  10. 【Bzoj1875】HH去散步

    [Bzoj1875]HH去散步 先说一下边点互化的思路(貌似这种题不多?),以后看见边数少的要死的记得想边点乎化,将无向边变成有向边在考虑边之间的可达性,如果边x的终点是边y的起点(前提不是同一条边) ...