ASP.NET MVC 5中的属性授权

时间:2018-04-22 20:39:27

标签: c# asp.net asp.net-mvc

我的Models文件夹

中有以下课程
namespace School.Models
{
        public enum Grade
        {
            A, B, C, D, F
        }

        public class Enrollment
        {
            public int EnrollmentID { get; set; }
            public int CourseID { get; set; }
            public int StudentID { get; set; }
            public Grade Grade { get; set; }

            public virtual Course Course { get; set; }
            public virtual Student Student { get; set; }
        }
}

我想在Grade属性中进行授权,因此当Admin中的Admin Role日志可以修改成绩值时,当用户角色登录时,用户无法修改属性并且只能看到成绩的值(以及如何为成绩属性设置默认值

1 个答案:

答案 0 :(得分:0)

在更新成绩的控制器操作上添加authorize属性。

[Authorize(Roles = "user or group")]
    public IActionResult UpdateGrade()
    {
        //update grade  here

        return View();
    }

在类构造函数中设置默认等级。

public class Enrollment
{

    public Enrollment()
    {
        Grade = Grade.B;
    }

    public int EnrollmentID { get; set; }
    public int CourseID { get; set; }
    public int StudentID { get; set; }
    public Grade Grade { get; set; }

    public virtual Course Course { get; set; }
    public virtual Student Student { get; set; }
}