• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本系列的的角色权限管理主要采用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】

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel.DataAnnotations;
 4 using System.ComponentModel.DataAnnotations.Schema;
 5 using System.Data.Entity;
 6 using System.Globalization;
 7 using System.Web.Security;
 8 
 9 namespace MVCSystem.Web.Models
10 {
11    
12 
13     public class RegisterExternalLoginModel
14     {
15         [Required]
16         [Display(Name = "用户名")]
17         public string UserName { get; set; }
18 
19         public string ExternalLoginData { get; set; }
20     }
21 
22     public class LocalPasswordModel
23     {
24         [Required]
25         [DataType(DataType.Password)]
26         [Display(Name = "当前密码")]
27         public string OldPassword { get; set; }
28 
29         [Required]
30         [StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
31         [DataType(DataType.Password)]
32         [Display(Name = "新密码")]
33         public string NewPassword { get; set; }
34 
35         [DataType(DataType.Password)]
36         [Display(Name = "确认新密码")]
37         [Compare("NewPassword", ErrorMessage = "新密码和确认密码不匹配。")]
38         public string ConfirmPassword { get; set; }
39     }
40 
41     public class LoginModel
42     {
43         [Required]
44         [Display(Name = "用户名")]
45         public string UserName { get; set; }
46 
47         [Required]
48         [DataType(DataType.Password)]
49         [Display(Name = "密码")]
50         public string Password { get; set; }
51 
52         [Display(Name = "记住我?")]
53         public bool RememberMe { get; set; }
54     }
55 
56     public class RegisterModel
57     {
58         [Required]
59         [Display(Name = "用户名")]
60         public string UserName { get; set; }
61 
62         [Required]
63         [StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
64         [DataType(DataType.Password)]
65         [Display(Name = "密码")]
66         public string Password { get; set; }
67 
68         [DataType(DataType.Password)]
69         [Display(Name = "确认密码")]
70         [Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
71         public string ConfirmPassword { get; set; }
72     }
73 
74     public class ExternalLogin
75     {
76         public string Provider { get; set; }
77         public string ProviderDisplayName { get; set; }
78         public string ProviderUserId { get; set; }
79     }
80 }
View Code

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(250)]
        public string UserName { get; set; }

        [Display(Name = "邮件地址")]
        [StringLength(250)]
        public string Email { get; set; }
    }
}
View Code

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; }
        }
    }
View Code

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; }
    }
}
View Code

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(256)]
            public string RoleName { get; set; }

            public ICollection<M_Membership> T_Members { get; set; }

            [ForeignKey("RoleId")]
            public ICollection<M_PermissionsInRoles> T_PermissionsInRoles { set; get; }
       
    }
}
View Code

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; }
    }
}
View Code

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; }
    }
}
View Code

 

然后创建一个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; }
    }
}
View Code

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

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

QQ:470797533

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
通过标准的ASP.NET控件来使用jQueryUI发布时间:2022-07-10
下一篇:
Asp.NetCore中使用Newtonsoft.Json进行序列化处发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap