I created MVC web app using ASP.NET 4.5 Templates. In Web.config, I changed the SQL Connection string to point my existing SQL Server database.
I run the project and created necessary tables into my SQL Server Database. I also create a new user.
My first questions is how to create a user role? And my Second question is how to use “Roles” to give respected permission to my user to access only certain part of my app?
Kind Regards,
Ok. Here is how to created roles.
I am using VS2015 and ASP.NET 4.5 MVC. I use Microsoft Asp.Net 4.5 identity for authentication.
(context.Roles.Add(new icrosoft.AspNet.Identity.EntityFramework.IdentityRole())
So in my Model Role.cs:
// POST: /Roles/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
context.Roles.Add(new Microsoft.AspNet.Identity.EntityFramework.IdentityRole()
{
Name = collection["RoleName"]
});
context.SaveChanges();
ViewBag.ResultMessage = "Role created successfully !";
return RedirectToAction("Index");
}
catch
{
return View();
}
}
And I also put below code top of my every controller respectivly:
[Authorize(Roles = "TestRole")]