Logging registered users

Jun 20 2017 6:12 PM
Hello,
 
I wonder if anyone is familiar with this article:
 
http://www.c-sharpcorner.com/article/asp-net-core-mvc-authentication-and-role-based-authorization-with-asp-net-core/
 
I'm almost done with it but I have a problem when my registered users logs in.
 
Whenever a user logs in, it always redirects it to the access denied page. Here is the Account Controller methods:
 
 
  1. [HttpPost]  
  2.         [AllowAnonymous]  
  3.         [ValidateAntiForgeryToken]  
  4.         public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)  
  5.         {  
  6.             ViewData["ReturnUrl"] = returnUrl;  
  7.             if (ModelState.IsValid)  
  8.             {  
  9.                 var result = await signInManager.PasswordSignInAsync(model.UserName, model.Password,   
  10.                     model.RememberMe, lockoutOnFailure: false);  
  11.                 if (result.Succeeded)  
  12.                 {  
  13.                     return RedirectToLocal(returnUrl);  
  14.                 }  
  15.                 else  
  16.                 {  
  17.                     ModelState.AddModelError(string.Empty, "Invalid login attempt.");  
  18.                     return View(model);  
  19.                 }  
  20.             }  
  21.             return View(model);  
  22.         }  
  23.   
  24. private IActionResult RedirectToLocal(string returnUrl)  
  25.         {  
  26.             if (Url.IsLocalUrl(returnUrl))  
  27.             {  
  28.                 return Redirect(returnUrl);  
  29.             }  
  30.             else  
  31.             {  
  32.                 return RedirectToAction(nameof(HomeController.Index), "Home");  
  33.             }  
  34.         }  
  35.   
  36.         public IActionResult AccessDenied()  
  37.         {  
  38.             return View();  
  39.         }  
 Anytime I debug the application, and when I log in with a user, It goes to the Access Denied view.
 
According to the article:
 
Now, run the Application and login with the valid credentials. Its authentication is successful. This authenticates the user, who doesn’t have ‘User’ roles due to which it’s not authorized to access Index method of HomeController and is being redirected on access denied 
 
But actually, I don't see anywhere a logic where the application validates if the user has a role or not, so maybe that is the reason.
 
Notice in this picture that altough the user is logged, and has a role, it goes to Access Denied:
 
 
Is the article wrong? Any advice is appreciated. Thanks.
 
 

Answers (3)