0
Good afternoon, I’m trying to use Identity in my API, but I have some error that I believe is dependency injection.
I made all the necessary configuration, connected with the bank, managed the Migrations, but when I try to inject the classes Signinmanager and Usermanager of Microsoft.AspNetCore.Identity in my Controller, I am taking an error of Argumentnullexception in Startup, as well as image below.
The error of understanding that the application is not able to access the string inside the appsettings, but to apply the Migrations is working.
If I remove Signinmanager and Usermanager from the Controller the error stops. Follow implementations:
Startup:
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
Appdbcontext:
namespace Identity.API.Data
{
public class AppDbContext : IdentityDbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) :
base(options) {}
}
}
appsettings (left half shaped):
{
"ConnectionStrings": {
"DefaultConnection":
"Server= (localdb)\\mssqllocaldb;Database=MyDb;
Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
Controller (I’m testing a simple GET method):
namespace Identity.API.Controllers
{
[ApiController]
[Route("auth")]
public class AuthController : Controller
{
public readonly SignInManager<IdentityUser> SignInManager;
public readonly UserManager<IdentityUser> UserManager;
public AuthController(
SignInManager<IdentityUser> signInManager,
UserManager<IdentityUser> userManager)
{
SignInManager = signInManager;
UserManager = userManager;
}
[HttpGet]
public string Get()
{
return "Deu certo";
}
}
}
You checked whether the method
GetConnectionString()
is returning the correct value? The database and tables were created in the database?– Leandro Angelo
Yes, they were created normally. Any Migration that I apply, of course. Even if I pass the direct connection string in Usesqlserver() of the same error, so I’m not getting the error.
– Bruno Inácio
Guys, the error happens when I use VS Code Bugger. When I run through the dotnet run terminal the error does not occur. Now I’ll find out why.
– Bruno Inácio
is not the environment variable?
– Leandro Angelo