ASP.net MVC Membership Forwarding

I have two types of roles [Admin, HelpDeskAdmin].

I have one login view (both users follow the same login link), and I want to check their role after logging in and redirect to their respective admin pages after authentication. The code below does not identify the registered user as being in the role for the first time and reloads the login page, the second time when he logs in, he redirects correctly.

Is this because the authentication cookie does not take place when it checks for the first time? How can I execute this script?

[HttpPost] public ActionResult LogOn(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { if (MembershipService.ValidateUser(model.UserName, model.Password)) { FormsService.SignIn(model.UserName, model.RememberMe); if (!String.IsNullOrEmpty(returnUrl)) { return Redirect(returnUrl); } else { if (Roles.IsUserInRole("Admin")) { //go to admin landing page return RedirectToAction("Index", "Manage"); } else if (Roles.IsUserInRole("HelpDesk")) { //go to helpdesk landing page return RedirectToAction("Index", "Interview"); } else /******FIRST TIME THROUGH IT ALWAYS GOES HERE *******/ return RedirectToAction("Index", "Home"); //not in any of those roles } } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); } } // If we got this far, something failed, redisplay form return View(model); } 
+4
source share
1 answer

The user is technically NOT logged in until the next request is processed and Cookie Authenification is set ....

Either follow the redirect logic in another action method, or perhaps Roles.IsUserInRole(model.UserName, "Admin") user information using Roles.IsUserInRole(model.UserName, "Admin") [[Note with username ]] from this action.

+7
source

All Articles