0

I have a controller that I want to set custom settings for people to be able to see it.

Controller

    [Authorize(UserRoles.Admin)]
    public ActionResult Index()
    {
        return View();
        //SqlConnect(url);

    }

Model

public enum UserRoles
{
    Admin,
    Employee,
    Guest
}
public class RegisterViewModel
{
    [Required]
    [Display(Name = "User Role")]
    public UserRoles UserRoles { get; set; }
}

I keep seeing syntax that say that this is the correct way to do it, but I can't get it to work. Help please and thank you.

Mathew Dodson
  • 115
  • 1
  • 1
  • 10
  • similar: https://stackoverflow.com/questions/1148312/asp-net-mvc-decorate-authorize-with-multiple-enums – user2404597 Sep 06 '18 at 18:54
  • I'm confused as to where the custom attribute goes. The only leads I get are to metadata. – Mathew Dodson Sep 06 '18 at 19:15
  • In the first sample code, if you want to restrict access to that method in your controller to only 'Admin'. Then, you can use this [Authorize(Roles = "Admin")] That data annotation will allow only users with a registered role in the Admin role. If you want to allow other roles add them inside the quotes separated with a comma. – nocturns2 Sep 06 '18 at 19:47
  • You might want to show your razor code to show how you're using the enum. That way others here can have a better idea of what you're trying to do and help you find a solution. – nocturns2 Sep 06 '18 at 19:51

1 Answers1

-2

This is incorrect syntax:

public UserRoles UserRoles { get; set; }

You can't name a variable the same name as the class, try instead:

public UserRoles _userRoles { get; set; }

Let me know if that helps.

poolfork
  • 42
  • 6