Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have designed a basic application that has 3 roles:

* Admin
* Clerk
* Donor

On startup of the project the user is directed to the Login/Register view. If a user login as an admin he should be redirect to the admin view that has an AdminLayoutPage.cshtml. If a donor login he should be redirected to the Donor view with DonorLayoutPage.cshtml. However I dont get the desired result. I have tried methods in the login controller to validate:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<actionresult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null && User.IsInRole("Admin"))
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}

// If we got this far, something failed, redisplay form
return View(model);
}
Posted
Updated 12-Aug-14 2:25am
v2
Comments
Nathan Minier 12-Aug-14 8:38am    
Your issue is more likely in your Role Provider than in your controller. This article hits all the points of how to wire up a role provider:
http://www.codeproject.com/Articles/607392/Custom-Role-Providers
ZainNabi 12-Aug-14 9:10am    
As per the link above, I see that FormsAuthentication is being used rather that identity

1 solution

OK, so how are you tracking who is in what role? If you're using the default provider and database then this code should work unless the user is not in the Admin role.

My personal opinion is to RedirectToAction that shows the Admin view instead of RedirectToLocal. That way you don't have to worry about URLs at all.

And what's with all the damn Async stuff on what is, by nature, a synchronous operation in the first place??
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900