首页 > 解决方案 > 在 api 中访问 userManager

问题描述

我在 ASP.Net 核心中有一个应用程序,分为 3 个项目(API、数据、服务)。API 包含将调用服务项目中的方法的控制器(我将在其中处理数据)。

现在我正在使用 ASP 提供的身份验证系统,所以我在启动方法中添加了这些行。

services.AddIdentity<IdentityUser,IdentityRole>().AddEntityFrameworkStores<RHPDbContext>();
services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
    options.SaveToken = true;
    options.RequireHttpsMetadata = false;
    options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
    {
        ValidateIssuer = true,
        ValidateAudience = true,
        ValidAudience = "RHP User",
        ValidIssuer = "MYIssuer",
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("MyRHPSuperKey"))
    };
});

由于我正在制作一个 API,我将需要基于令牌的身份验证

并且还在启动的configure方法中添加了这一行

app.UseAuthentication();

我现在的问题是如何访问userManager我的服务项目中的,以便我可以添加用户,或获取用户,将角色属性分配给用户......等等

标签: c#asp.netasp.net-mvcasp.net-coreasp.net-web-api

解决方案


这是您可以在控制器类中访问 userManager 的方式。

public AccountController(UserManager<IdentityUser> userManager)


推荐阅读