Goold eng
Goold eng

Reputation: 31

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

Upvotes: 3

Views: 14040

Answers (2)

crgolden
crgolden

Reputation: 4634

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

Upvotes: 2

StepUp
StepUp

Reputation: 38094

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.

Upvotes: 7

Related Questions