Unable to connect to database to perform authentication functions

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));

    // Student
    if (!RoleManager.RoleExists("Student"))
    {
        RoleManager.Create(new IdentityRole("Student"));
    }

    // Teacher
    if (!RoleManager.RoleExists("Teacher"))
    {
        RoleManager.Create(new IdentityRole("Teacher"));
    }

    // Administrator
    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));


    // Create a Student user and set it role to Student
    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");
    }


    // Create a Teacher user and set it role to Teacher
    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");
    }


    // Create a Administrator user and set it role to Administrator
    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
    }
+2
1

, ASP.NET, ASP.NET, :

<system.web>
    <roleManager enabled="true" />
</system.web>

SqlRoleProvider, SQL Express -\app_dir.

machine.config, :

<connectionStrings>
  <add name="LocalSqlServer" 
    connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
      AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
      providerName="System.Data.SqlClient"/>
</connectionStrings>
.
.
.
<roleManager>
  <providers>
    <add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer" ...
</providers>
</roleManager>

, SQL Express, - SQL.

ASP.NET System.Web.Security.Roles ( ASP.NET), Microsoft.AspNet.Identity.RoleManager.

+4

All Articles