I read a lot of articles and forums, but I still canโt figure it out ... I am creating an Internet application using Visual Studio Express 2012 for the Internet, with MVC4 + Razor + Entity Framework CodeFirst.
As I understand it, managing users and roles in MVC4 using SimpleMembership is more straightforward than in previous versions, and should be fairly simple.
In my application, I need to authorize only certain user groups (for example, only admins can access certain pages). I understand that this is done by passing the parameter to the annotation [Authorize]: [Authorize (Roles = "Admins")] But how do I create these roles and how to add users to them?
To require authentication, I added the [Authorize] annotation (without parameters) on top of the controller method, and it worked without any additional configurations or adding anything else. Also, when I look at the database that was automatically created, I see a table called webpages_UsersInRoles, with UserId and RoleId columns. All this makes me think that this should be a fairly simple task, since everything seems configured and ready to use, but I just can not figure out how;)
What I tried so far (and this did not work) was as follows: I have a DataContextDbInitializer class that inherits from DropCreateDatabaseIfModelChanges. The Seed method is overridden inside this class, and I added this (I had to import System.Web.Security):
Membership.CreateUser("user1", "123456"); Roles.CreateRole("Admins"); Roles.AddUserToRole("user1", "Admins");
I also added this tag to the tag of the Web.config file:
<roleManager enabled="true" cacheRolesInCookie="true" > </roleManager>
To try, I added [Authorize (Roles = "Admins")] on top of the action method in the controller, and then logged in as "admin" and tried to access this method, but no luck :(
I donโt know what else Iโm missing ... I would be very happy if someone could direct me to this, since it was driving me crazy: P
Thanks!