C# 是否在Asp.net核心中获取UserManager类的对象? 使用系统; 使用制度; 使用System.Collections.Generic; 使用System.Linq; 使用System.Security.Claims; 使用System.Threading.Tasks; 使用Microsoft.AspNetCore.Identity; 使用服务和网站模型; 使用Microsoft.AspNetCore.Mvc; 使用服务和网站数据; 使用Microsoft.AspNetCore.Identity.EntityFrameworkCore; 命名空间服务\u网站.CustomFunctions { 公共类CustomUserOperations:控制器 { 私人用户管理器(UserManager);; 公共应用程序用户GetCurrentUser() { ClaimsPrincipal currentUser=用户; _userManager=newusermanager(); var user=\u userManager.GetUserAsync(user).Result; 返回用户; } } }

C# 是否在Asp.net核心中获取UserManager类的对象? 使用系统; 使用制度; 使用System.Collections.Generic; 使用System.Linq; 使用System.Security.Claims; 使用System.Threading.Tasks; 使用Microsoft.AspNetCore.Identity; 使用服务和网站模型; 使用Microsoft.AspNetCore.Mvc; 使用服务和网站数据; 使用Microsoft.AspNetCore.Identity.EntityFrameworkCore; 命名空间服务\u网站.CustomFunctions { 公共类CustomUserOperations:控制器 { 私人用户管理器(UserManager);; 公共应用程序用户GetCurrentUser() { ClaimsPrincipal currentUser=用户; _userManager=newusermanager(); var user=\u userManager.GetUserAsync(user).Result; 返回用户; } } },c#,asp.net,asp.net-core,asp.net-mvc-5,asp.net-core-mvc,C#,Asp.net,Asp.net Core,Asp.net Mvc 5,Asp.net Core Mvc,*这是我的控制器* 我只想得到UserManager类的一个对象,但当我这样做时,我得到了这个错误 错误CS7036给定的参数没有对应于“UserManager.UserManager”所需的形式参数“store”(IUserStore、IOOptions、IPasswordHasher、IEnumerable>、IEnumerable>、ILookupNormalizer、IdentityErrorDescriber、IServiceProvider、ILogger>)'服务\u网站F:\we

*这是我的控制器*

我只想得到UserManager类的一个对象,但当我这样做时,我得到了这个错误

错误CS7036给定的参数没有对应于“UserManager.UserManager”所需的形式参数“store”(IUserStore、IOOptions、IPasswordHasher、IEnumerable>、IEnumerable>、ILookupNormalizer、IdentityErrorDescriber、IServiceProvider、ILogger>)'服务\u网站F:\webApplication\CoreApp\Services\u网站\Services\u网站\CustomFunctions\CustomUserOperations.cs 75处于活动状态*


*错误源是什么

可以使用依赖项注入来获取
UserManager
类的实例。只需将参数添加到
Startup.cs
中的
Configure
方法

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;
        }
    }
}

public void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、,

用户管理器。

您可以像这样设置身份,例如:

public void配置服务(IServiceCollection服务)
{
services.AddDefaultIdentity

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
    UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{}
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;
     }
}