1

In my AccountController, I have:

    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                if (Roles.IsUserInRole(model.Email, "Student")) <-- Here is where error is thrown
                {
                    RedirectToAction("DriveSignUp", "Home");
                }
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
    }

I am currently using Roles throughout the site, but in adding the code in the Success portion of the switch statement, I get an error stating the Role Manager Feature has not been enabled.

Any idea why this would throw that error here?

Keith Clark
  • 609
  • 1
  • 7
  • 19

2 Answers2

1

Do you have the role manager enabled? See: RoleManager

System.Web.Security.Roles.Enabled

This should be enabled as an attribute in the roleManager element in the web.config:

<configuration>
  <system.web>
    <roleManager enabled="true" />
  </system.web>
</configuration>
Community
  • 1
  • 1
Mark
  • 4,773
  • 8
  • 53
  • 91
  • No, I dont, but I am using `if (User.IsInRole("ROLE"))` all through out my code without any problems. If I add the roleManager statement from above to web.config, the app throws a "cannot connect to SQL" any time `User.IsInRole` is called – Keith Clark Oct 25 '16 at 20:16
0

I'm guessing you're changing your security either from Membership to ASP.NET Identity or going up on MVC version completely? In any case, you probably will want to use new ASP.NET Identity features, as System.Web.Security (part of which is that Roles class) is no longer in use here. If yes, check the answers here for more details: MVC 5 - Roles - IsUserInRole and Adding user to role

Community
  • 1
  • 1
patrykgliwinski
  • 316
  • 1
  • 8