Problem using Identity in API ASP . NET Core

Asked

Viewed 75 times

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.

ArgumentNullException

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?

  • 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.

  • 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.

  • is not the environment variable?

1 answer

0

If you are using Asp.net core 3.1

The Startup:

services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<MeuDbContext>();

See if you have installed

<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="3.1.7" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.11" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.