3
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

1

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.