在asp.net MVC中首先使用Code分配页面的权限: [英] Assign permission for pages using Code first in asp.net MVC:

查看:103
本文介绍了在asp.net MVC中首先使用Code分配页面的权限:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Quote:



这里我在asp.net mvc中创建了一个模型结构:



公共类UserModel

{

public int UserId {get;组; } $ / $
公共字符串UserName {get;组; }

公共字符串密码{get;组; }

public List< permission>权限{get;组; } $ / $
公共字符串FirstName {get;组; }

公共字符串LastName {get;组; }

}



公共类权限

{

public int PermissionID {得到;组; }

public bool IsPermit {get;组; }

public string Name {get;组; }

}



并在列表中设置一些默认值,当我在列表中添加用户时,我为页面分配了权限通过UI(通过检查权限复选框)向该用户,以便用户只能访问指定的页面:



公共静态类存储库

{

public static List< usermodel> GetUsers()

{

List< usermodel> listUsers = new List< usermodel>

{

new UserModel

{

UserId = 1,
UserName =abc,

密码=abc,

权限=新列表<权限>

{

新许可

{

PermissionID = 1,

IsPermit = true,

姓名=Page1

},

新许可

{

PermissionID = 2,

IsPermit = false,

Name =Page2

},

n ew许可

{

PermissionID = 3,

IsPermit = false,

Name =Page3

},

新许可

{

PermissionID = 4,

IsPermit = false ,

名称=Page4

}

},

FirstName =Rohit,

LastName =Sharma

},



新用户模型

{

UserId = 2,

UserName =xyz,

密码=xyz,

Permissions = new List<烫发ission>

{

新许可

{

PermissionID = 1,

IsPermit = false,

Name =Page1

},

新许可

{

PermissionID = 2,

IsPermit = true,

Name =Page2

},

新许可

{

PermissionID = 3,

IsPermit = true,

Name =Page3< br $>
},

新许可

{

PermissionID = 4,

IsPermit = true,

Name =Page4

}

},

FirstName =Rahul,

LastName =Sharma

}

};



返回列表用户;

}

}



现在我想通过在DbContext类的帮助下使用数据库的代码优先方法来做同样的事情。

我在数据库表中有一个静态的页面权限列表(Id = 1,Name = Page1; Id = 2,Name = Page2; Id = 3,Name = Page3; Id = 4,Name = Page 4)。



我在为数据库创建模型结构时感到困惑。请指导我如何创建模型结构和表格结构的映射。



请尽快回复。

谢谢。

解决方案

让我们试着帮助



  public   class  UserModel 
{
public int UserId { get ; set ; }
public string UserName { get ; set ; }
public string 密码{ get ; set ; }
public virtual 列表< permissions>权限{获取; set ; }
public string FirstName { get ; set ; }
public string LastName { get ; set ; }
}

public class 权限
{
public int PermissionID { get ; set ; }
public bool IsPermit { get ; set ; }
public string 名称{ get ; set ; }
public virtual UserModel用户{ get ; set ;}
public int UserId { get ; set ;}
}
< span class =code-keyword>< /
权限 >







因此,我们可以通过Fluent验证来映射这些实体



  public   class  UserModelMap:EntityTypeConfiguration< usermodel> 
{
public UserModelMap()
{
// 主键
.HasKey(m => m.UserId);

// 属性
// 表&列映射
.ToTable( 的usermodel);
.Property(m => m.UserName).IsRequired();
.Property(m => m.Password).IsRequired();
.Property(m => m.UserName).HasColumnName( 用户名);
.Property(m => m.Password).HasColumnName( 主体);

// 为简洁省略了其他财产
}
} < / usermodel >





  public   class  PermissionMap:EntityTypeConfiguration< permission> 
{
public PermissionMap()
{
// 主键
.HasKey(m => m.PermissionID);

// 属性
// 表&列映射
.ToTable( 权限);
.Property(m => m.IsPermit).IsRequired();
this .Property(m => m.Name).IsRequired();
.Property(m => m.IsPermit).HasColumnName( IsPermited);
.Property(m => m.Name).HasColumnName( 名称);

// 其他财产因为简洁而被忽略
this .HasRequired(p = > p.User).WithMany(um = > um.Permissions)
.HasForeignKey(c = > c.UserId);
}
} < / 权限 >





  public   class  MyContext:DbContext 
{
public MyContext(): base (name = conStrName) {}

受保护 覆盖 void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add( new PermissionMap());
modelBuilder.Configurations.Add( new UserModelMap());
}

public DbSet< usermodel>用户{获取; set ; }
public DbSet< permission>权限{获取; set ; }

} < / 权限 > < / usermodel >


Quote:

Hi,
Here I have created a Model Structure in asp.net mvc:

public class UserModel
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public List<permission> Permissions { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

public class Permission
{
public int PermissionID { get; set; }
public bool IsPermit { get; set; }
public string Name { get; set; }
}

and setting some default values in the list and while I am adding the user in the list I assign the permission for pages to that user through the UI (by checking permission checkboxes), so that user can access only the assigned pages:

public static class Repository
{
public static List<usermodel> GetUsers()
{
List<usermodel> listUsers = new List<usermodel>
{
new UserModel
{
UserId = 1,
UserName = "abc",
Password = "abc",
Permissions = new List<permission>
{
new Permission
{
PermissionID = 1,
IsPermit = true,
Name = "Page1"
},
new Permission
{
PermissionID = 2,
IsPermit = false,
Name = "Page2"
},
new Permission
{
PermissionID = 3,
IsPermit = false,
Name = "Page3"
},
new Permission
{
PermissionID = 4,
IsPermit = false,
Name = "Page4"
}
},
FirstName = "Rohit",
LastName = "Sharma"
},

new UserModel
{
UserId = 2,
UserName = "xyz",
Password = "xyz",
Permissions = new List<permission>
{
new Permission
{
PermissionID = 1,
IsPermit = false,
Name = "Page1"
},
new Permission
{
PermissionID = 2,
IsPermit = true,
Name = "Page2"
},
new Permission
{
PermissionID = 3,
IsPermit = true,
Name = "Page3"
},
new Permission
{
PermissionID = 4,
IsPermit = true,
Name = "Page4"
}
},
FirstName = "Rahul",
LastName = "Sharma"
}
};

return listUsers;
}
}

Now I want to do the same by using code first approach of database with the help of DbContext class.
I have a static list of page permission in a database table (Id =1, Name=Page1; Id =2, Name=Page2; Id =3, Name=Page3; Id =4, Name=Page4).

I am confused while creating model structure for database. Please guide me how to create model structure and mapping of structure with the tables.

Please reply ASAP.
Thanks.

解决方案

Hi , let''s try to help

public class UserModel
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public virtual List<permissions> Permissions { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

public class Permission
{
public int PermissionID { get; set; }
public bool IsPermit { get; set; }
public string Name { get; set; }
public virtual UserModel User {get;set;}
public int UserId {get;set;}
}
</permissions>




So lets mapping this entities via Fluent Validation

public class UserModelMap : EntityTypeConfiguration<usermodel>
    {
        public UserModelMap()
        {
            // Primary Key
            this.HasKey(m =>m.UserId );

            // Properties
            // Table & Column Mappings
            this.ToTable("UserModel");
            this.Property(m =>m.UserName).IsRequired();
            this.Property(m =>m.Password).IsRequired();
            this.Property(m =>m.UserName).HasColumnName("UserName");
            this.Property(m =>m.Password).HasColumnName("Subject");
            
            // other propertise was ommited for brevity
        }
    }</usermodel>



public class PermissionMap:EntityTypeConfiguration<permission>
    {
        public PermissionMap()
        {
           // Primary Key
            this.HasKey(m =>m.PermissionID );

            // Properties
            // Table & Column Mappings
            this.ToTable("Permissions");
            this.Property(m =>m.IsPermit ).IsRequired();
            this.Property(m =>m.Name ).IsRequired();
            this.Property(m =>m.IsPermit).HasColumnName("IsPermited");
            this.Property(m =>m.Name ).HasColumnName("Name");

            // other propertise was ommited for brevity
            this.HasRequired(p => p.User).WithMany(um => um.Permissions)
				.HasForeignKey(c => c.UserId);
        }
    }</permission>



public class MyContext:DbContext
{
   public MyContext():base(name="conStrName"){}
   
   protected override void OnModelCreating(DbModelBuilder modelBuilder)
   {
       modelBuilder.Configurations.Add(new PermissionMap());
       modelBuilder.Configurations.Add(new UserModelMap());
   }

   public DbSet<usermodel> Users { get; set; }
   public DbSet<permission> Permissions { get; set; }

}</permission></usermodel>


这篇关于在asp.net MVC中首先使用Code分配页面的权限:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆