Restrict Login only to specific Role(s)


is it possible to Restrict Login only to specific Role(s)?
how to do this (in the AccountController)?


You can modify the Login method in AccountController to check for the required roles.

var roles = await userManager.GetRolesAsync(user);

if (!roles.Contains("Administrator"))
   return RedirectWithError("User is not an administrator", redirectUrl);


how to get the user object after successfully logged in (result.Succeeded)

the following code resturns always null...

var user = await userManager.GetUserAsync(User);


The Login method contains this code:

var user = await userManager.FindByNameAsync(userName);
1 Like