Asp.Net Core 6 Scoped Filter inject UserManager

504 Views Asked by At

I'm working on linking Twilio Verify into an Asp.Net Core web site. I'm pretty sure I have to figure out how to access UserManager in the filter (constructor). But, I don't know how to access it.

My VerifyFilter:

public class VerifyFilter : IAsyncResourceFilter
{
   private readonly UserManager<ApplicationUser> _manager;

   public VerifyFilter(UserManager<ApplicationUser> manager)
   { _manager = manager; }

   public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
   { // cut just to make listing a bit shorter }
}

My program.cs file currently looks like this:

builder.Services.AddScoped<VerifyFilter>();

What I don't know is how I get the UserManager so I can pass it in.

I have another scoped right above it and I had to do this to get Verification to work.

Configuration.Twilio twilio = new Configuration.Twilio();
builder.Services.AddScoped<IVerification>(t => new Verification(twilio));

So I'm sure it's just a matter of figuring out how to get UserManager to pass in as a constructor, but with MinimalAPI and .Net Core 6.0, I don't know where it is at.

My entire program.cs file:

using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using test4.Data;
using test4.Filters;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlite(connectionString));

builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services
    .AddDefaultIdentity<IdentityUser>(
        options =>
        {
           // These will get updated to production versions later.
           options.SignIn.RequireConfirmedAccount = true;
           options.Password.RequiredLength = 1;
        })
    .AddEntityFrameworkStores<ApplicationDbContext>();

builder.Services.AddRazorPages();

builder.Services.AddScoped<VerifyFilter>();

builder.Services.AddControllers(op =>
{
   op.Filters.Add<VerifyFilter>();
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
   app.UseMigrationsEndPoint();
}
else
{
   app.UseExceptionHandler("/Error");
   // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
   app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages();

app.Run();

Any suggestions?

Thanks, Nick

1

There are 1 best solutions below

3
Zhi Lv On

You could try to register the filter via the following code:

//register the Identity service

//register the custom filters.
builder.Services.AddControllers(op =>
{
    op.Filters.Add<VerifyFilter>();
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();