I created a new application with ASP.NET MVC5, using individual user accounts for security and code-first conversion for modeling models / databases. All options are by default.
I want to set up custom users and roles for it, so I created a seed using RoleManagerand UserManagerto populate the database. It works great, creates 3 users, 3 roles and correctly installs each user role.
I can enter the application correctly. The problem is that I cannot execute any Identity method using a
User who is a registered user, the only thing I can get is the value User.Identity.IsAuthenticatedand User.Identity.Namewhich are correct. Every other method, such as Roles.GetRolesForUser("John")or User.IsInRole("Student"), always throws this exception:
ProviderException: Role Manager function is not enabled.
As good as in this article , I managed to insert <roleManager enabled="true" />into Web.config, and now it takes a very long response time and throws exceptions, such as:
Turning to:
Roles.IsUserInRole("Student")
Forms:
HttpException
Turning to:
Roles.GetRolesForUser(User.Identity.Name)
Forms:
NullReferenceException
Access to the About page with user participation as a student:
[Authorize(Roles="Teacher")]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
Forms:
SqlException
HttpException
, , LocalDB, Seed .
AspNetUsers , :
using (Models.ApplicationDbContext db = new Models.ApplicationDbContext())
{
List<Models.ApplicationUser> Users = db.Users.ToList();
ViewBag.Users = Users;
}
return View();
, : '(
, , Identity.
protected override void Seed(LocalBDAuthTest.Models.ApplicationDbContext context)
{
#region Create Roles Student, Teacher e Administrator
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
if (!RoleManager.RoleExists("Student"))
{
RoleManager.Create(new IdentityRole("Student"));
}
if (!RoleManager.RoleExists("Teacher"))
{
RoleManager.Create(new IdentityRole("Teacher"));
}
if (!RoleManager.RoleExists("Administrator"))
{
RoleManager.Create(new IdentityRole("Administrator"));
}
#endregion
#region Create Users for Student, Teacher e Administrator Roles.
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
if (!context.Users.Any(u => u.UserName == "John"))
{
var Student = new ApplicationUser() { UserName = "John" };
var Result = UserManager.Create(Student, "123456");
}
if (!UserManager.FindByName("John").Roles.Any())
{
UserManager.AddToRole(UserManager.FindByName("John").Id, "Student");
}
if (!context.Users.Any(u => u.UserName == "Arnold"))
{
var Teacher = new ApplicationUser() { UserName = "Arnold" };
var Result = UserManager.Create(Teacher, "123456");
UserManager.AddToRole(Teacher.Id, "Teacher");
}
if (!UserManager.FindByName("Arnold").Roles.Any())
{
UserManager.AddToRole(UserManager.FindByName("Arnold").Id, "Teacher");
}
if (!context.Users.Any(u => u.UserName == "Caroline"))
{
var Administrator = new ApplicationUser() { UserName = "Caroline" };
var Result = UserManager.Create(Administrator, "123456");
UserManager.AddToRole(Administrator.Id, "Administrator");
}
if (!UserManager.FindByName("Caroline").Roles.Any())
{
UserManager.AddToRole(UserManager.FindByName("Caroline").Id, "Administrator");
}
#endregion
}