How to direct each user to their page corresponding to their access profile using Asp.net Identity with roles

Asked

Viewed 162 times

1

I have 3 profile registered in the SQL Server database, they are Master, Admin and User , when log in as you would for each user to be directed to your permission view. by default the route ta loading the home/index.


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

        // Isso não conta falhas de login em relação ao bloqueio de conta
        // Para permitir que falhas de senha acionem o bloqueio da conta, altere para shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                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("", "Tentativa de login inválida.");
                return View(model);
        }
    }

how to implement if, tried and did not compile, with your information above. @Gilberto-b-terra-Jr

1 answer

0

Redirect by checking the User Role in the Accountcontroller login method?

UPDATED

switch (result) 
{
    case SignInStatus.Success:
        if (User.IsInRole("Master"))
            return RedirectToAction("Index", "Master");

        if (User.IsInRole("Admin"))
            return RedirectToAction("Index", "Admin");

        return RedirectToAction("Index", "Home");

    case SignInStatus.LockedOut:
        return View("Lockout");
    case SignInStatus.RequiresVerification:
        return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
    case SignInStatus.Failure:
    default:
        ModelState.AddModelError("", "Tentativa de login inválida.");
        return View(model);
}

Hello friend, this is a simple example, see if you can there, let us know if you can not yet! See that I put some if (User.Isrole( to redirect each user profile to different pages (controllers/actions)!

  • My Accountcontroller is like this.

  • @Marlonandrade I changed the code above with a very simple example, if you have any questions, let me know where you got it. Remember that it is most important you really understand the code, beware of cake recipes. ;:)

  • OK, I warn you. I’m almost done.. @Gilberto B. Terra Jr..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.