YOUSFI Mohamed Walid
YOUSFI Mohamed Walid

Reputation: 580

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

Upvotes: 0

Views: 1144

Answers (1)

Rafaqat Ali
Rafaqat Ali

Reputation: 718

Here is the way you can access userManager in your controller class.

public AccountController(UserManager<IdentityUser> userManager)

Upvotes: 1

Related Questions