【发布时间】:2012-06-01 00:23:00
【问题描述】:
在DB 中,我有Role 和User 具有一对多关系的实体。
我想做的是构建自定义授权过滤器。我看到的所有教程都使用默认的ASP.NET 成员资格。我只知道我需要继承AuthorizationAttribute,但不知道我需要重写哪些方法以及如何实现它们。
public class UserAuth : AuthorizeAttribute
{
}
在DB:
角色
public class Role
{
[Key]
public int RoleID { get; set; }
[Required]
public int RolenameValue { get; set; }
[MaxLength(100)]
public string Description { get; set; }
// // // // //
public Rolename Rolename
{
get { return (ProjectName.Domain.Enums.Rolename)RolenameValue; }
set { RolenameValue = (int)value; }
}
public virtual ICollection<User> Users { get; set; }
}
用户
public class User
{
[Key]
public int UserID { get; set; }
[Required]
[MaxLength(30)]
public string Username { get; set; }
[Required]
[MinLength(5)]
public string Password { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[MaxLength(30)]
public string FirstName { get; set; }
[MaxLength(50)]
public string LastName { get; set; }
[DataType(DataType.Date)]
public DateTime Birthdate { get; set; }
public int GenderValue { get; set; }
// // // // // // //
public Gender Gender
{
get { return (ProjectName.Domain.Enums.Gender)GenderValue; }
set { GenderValue = (int)value; }
}
public int RoleID { get; set; }
[ForeignKey("RoleID")]
public Role Role { get; set; }
【问题讨论】:
标签: asp.net asp.net-mvc c#-4.0 authorization custom-attributes