一种授权特定用户并将其分配给不同控制器或操作的方法:创建一个简单的静态类并创建 const 变量。
public static class RolesText
{
public const string MISDeveloper = "MIS Developer";
public const string SuperAdmin = "Super Admin";
public const string Admin = "Admin";
public const string DistrictMnE = "District M&E";
public const string PSUEngineer = "PSU Engineer";
public const string Guest = "Guest";
public const string All_Users = MISDeveloper + "," + SuperAdmin + "," + Admin + "," + DistrictMnE + "," + PSUEngineer + "," + Guest;
public const string Without_PSUEng_DistMnE = MISDeveloper + "," + SuperAdmin + "," + Admin + "," + Guest;
public const string Without_PSUEng_Guest = MISDeveloper + "," + SuperAdmin + "," + Admin + "," + DistrictMnE;
public const string Without_DistMnE_Guest = MISDeveloper + "," + SuperAdmin + "," + Admin + "," + PSUEngineer;
}
然后像下面这样调用这些值。
[Authorize(Roles = RolesText.Without_PSUEng_Guest)]
public class NeedBasedSMsController : Controller
{
private AUPDBEntities db = new AUPDBEntities();
}
如果要分配不同的用户,请按如下方式调用它。
[Authorize(Roles = RolesText.Admin + "," + RolesText.Guest)]
public class MyController : Controller
{
// YOUR CONTROLLER CLASS
}