Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an Object of UserManager Class in Asp.net Core?

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Services_Website.Models;
using Microsoft.AspNetCore.Mvc;
using Services_Website.Data;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

namespace Services_Website.CustomFunctions
{
    public class CustomUserOperations: Controller
    {

        private UserManager<ApplicationUser> _userManager;
        public ApplicationUser GetCurrentUser()
        {
            ClaimsPrincipal currentUser = User;
            _userManager = new UserManager<ApplicationUser>();
            var user =_userManager.GetUserAsync(User).Result;
            return user;
        }
    }
}

* This is my Controller *

I Just want to get an object of the UserManager Class But when I do it I am getting this ERROR

Error CS7036 There is no argument given that corresponds to the required formal parameter 'store' of 'UserManager.UserManager(IUserStore, IOptions, IPasswordHasher, IEnumerable>, IEnumerable>, ILookupNormalizer, IdentityErrorDescriber, IServiceProvider, ILogger>)' Services_Website F:\webApplication\CoreApp\Services_Website\Services_Website\CustomFunctions\CustomUserOperations.cs 75 Active *

*What is the Error source

like image 862
Goold eng Avatar asked Dec 06 '25 05:12

Goold eng


2 Answers

It is possible to use dependency injection to get an instance of UserManager class. Just add parameter to Configure method in Startup.cs.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
    UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{}

and then in controller:

public class CustomUserOperations: Controller
{
     private UserManager<ApplicationUser> _userManager;
     CustomUserOperations(UserManager<ApplicationUser> userManager)
     {
         _userManager = userManager;
     }

     public ApplicationUser GetCurrentUser()
     {
         ClaimsPrincipal currentUser = User;
         var user =_userManager.GetUserAsync(User).Result;
         return user;
     }
}

You can read more about dependency injection here.

like image 159
StepUp Avatar answered Dec 08 '25 19:12

StepUp


You can setup Identity like this for example:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDefaultIdentity<ApplicationUser>()
        .AddEntityFrameworkStores<ApplicationDbContext>();
}

public void Configure(IApplicationBuilder app)
{
    app.UseAuthentication();
}

That will tell your app to populate the claims on the ClaimsPrinciple, add the UserManager and RoleManager to your DI pipeline, and configure your ApplicationDbContext as the persistence store.

Then you can inject UserManager/RoleManager into any constructor you need to using the DI pipeline like this:

private readonly UserManager<ApplicationUser> _userManager;

public CustomUserOperations(UserManager<ApplicationUser> userManager)
{
    _userManager = userManager;
}

Reference: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity

like image 32
crgolden Avatar answered Dec 08 '25 17:12

crgolden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!