这是我现在做的事情,声明Roles
允许访问我的控制器操作方法的字符串:
[Role(Roles="Regular User, Accounting, Administrator")]
public ActionResult ChangeAvatar()
{
if (User.Identity.IsAuthenticated)
{
var user = _userRepository.FindUserByEmail(User.Identity.Name);
var model = new ChangeAvatarModel();
model.CurrentAvatarUrl = user.AvatarUrl;
return View(model);
}
return RedirectToAction("Login", "Account");
}
我不想将其写成Roles
字符串,因为我可能会输入错误并导致意外错误。我宁愿把它们写一次就完成了。
你建议我在这个用例中使用什么?一个dictionary<enum, string>
?
[Role(Roles=DefinedRoles.Accounting)] // Would return string: "Regular User, Account"
public ActionResult ChangeAvatar()
{
if (User.Identity.IsAuthenticated)
{
var user = _userRepository.FindUserByEmail(User.Identity.Name);
var model = new ChangeAvatarModel();
model.CurrentAvatarUrl = user.AvatarUrl;
return View(model);
}
return RedirectToAction("Login", "Account");
}