Understanding UserManager<IdentityUser> and UserManager<IdentityUser>

lesponce 176 Reputation points
2022-03-09T00:25:57.687+00:00

How do I get the values of the following objects:

UserManager<IdentityUser> userManager,
SignInManager<IdentityUser> signInManager,
IConfiguration configuration

I need userManager, signInManager, and configuration to call a method inside of a controller. The above parameters are part of the constructor.

How can I accomplish this?

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-03-10T02:56:32.307+00:00

    Hi @lesponce ,

    The UserManager, SignInManager and the RoleManager are the Asp.net core Identity build in class, and they provide the related methods to manage user and role.

    After configuring Identity services in the Program.cs (Asp.net 6) or Startup.cs (Asp.net core 1~3 and Asp.net 5), like this (the following sample code apply to asp.net 6 web application):

    //get the connctionstring via Configuration  
    var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");  
      
    //register database and DBcontext  
    builder.Services.AddDbContext<ApplicationDbContext>(options =>  
        options.UseSqlServer(connectionString));  
    builder.Services.AddDatabaseDeveloperPageExceptionFilter();  
    //register the Identity service  
    builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)  
        .AddRoles<IdentityRole>()  //add the role service.  
        .AddEntityFrameworkStores<ApplicationDbContext>();  
    

    Then, in the MVC controller or Razor page, we could use the constructor injection method to inject the UserManager, SignInManager and IConfiguration, and then use them like this:

    public class AccountController : Controller  
    {  
        private readonly IConfiguration _config;  
        private readonly UserManager<IdentityUser> _userManager;  
        public AccountController(IConfiguration configuration,   
            UserManager<IdentityUser> userManager)  
        {  
            _config = configuration;  
            _userManager = userManager;  
        }  
    
        public IActionResult Index()  
        {  
            var connectionstring = _config.GetConnectionString("DefaultConnection");  
    
            var user = _userManager.FindByNameAsync(User.Identity.Name);  
    
            return View();  
        }  
    

    The screenshot as below:

    181655-1.gif

    More detail information about using Asp.net core Identity, see Introduction to Identity on ASP.NET Core and Scaffold Identity in ASP.NET Core projects.

    To the use of IConfiguration, you can refer to Configuration in ASP.NET Core.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.