Here is how I’m doing things now, declaring the string of Roles allowed to access my controller action method:
[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");
}
I don’t want to write up the Roles as a string because I may mistype and cause an unexpected bug. I’d rather write them once and be done with it.
What do you suggestion I use for this use case? A 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");
}
You could use a static constants class: