简体   繁体   中英

Access userManager in api

I have an application in ASP.Net core divided into 3 projects (API, data, services). The API contains the controllers that will be calling the methods in the services project (that's where I'll be processing the data).

Right now I am using the Authentification system ASP provides, so I added those lines in the startup method.

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

Since I am making an API I will be needing the token-based authentification

And also added this line in the configure method in startup

app.UseAuthentication();

My question now is how can I access the userManager in my services project so I can add users, or get the users, attribute roles to users...etc

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

public AccountController(UserManager<IdentityUser> userManager)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM