Reputation: 5
I need to create login with web api but when i enter this url https://localhost:44366/api/login
show me this error :
InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[IRI.DataLayer.Context.ApplicationDbContext]' while attempting to activate 'IRI.DataLayer.Context.ApplicationDbContext'.
and then enter the URL https://localhost:44366/api/login/Authenticate
show me this error
This localhost page can’t be found No webpage was found for the web address: https://localhost:44366/api/login/Authenticate HTTP ERROR 404
what's the problem ? how can I solve this problem?
My code =>
LoginController :
[Route("api/[controller]")]
[ApiController]
public class LoginController : ControllerBase
{
private readonly IApplicationUserManager _userManager;
private readonly IApplicationSignInManager _signIn;
private readonly IOptionsSnapshot<SiteSetting> _options;
private readonly ILogger<LoginController> _logger;
public LoginController(IApplicationUserManager userManager
, IApplicationSignInManager signIn
, IOptionsSnapshot<SiteSetting> options
, ILogger<LoginController> logger)
{
_userManager = userManager;
_userManager.CheckArgumentIsNull(nameof(_userManager));
_options = options;
_options.CheckArgumentIsNull(nameof(_options));
_signIn = signIn;
_signIn.CheckArgumentIsNull(nameof(_signIn));
_logger = logger;
_logger.CheckArgumentIsNull(nameof(_logger));
}
public async Task<IActionResult> Authenticate(LoginViewModel model, string returnUrl = null)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByNameAsync(model.username);
if (user == null)
{
return BadRequest(Messages.IncorrectUsernamePassword);
}
if (!user.IsActive)
{
return BadRequest(Messages.NotActive);
}
if (_options.Value.EnableEmailConfirmation
&& await _userManager.IsEmailConfirmedAsync(user))
{
return BadRequest(Messages.EmailConfirmation);
}
}
var result = await _signIn.PasswordSignInAsync(
model.username,
model.password,
model.rememberme,
lockoutOnFailure: true);
if (result.Succeeded)
{
_logger.LogInformation(1, $"{model.username} logged in");
return Ok(User);
}
if (result.RequiresTwoFactor)
{
//TODO Create Function for TowFactor
}
if (result.IsNotAllowed)
{
return BadRequest(Messages.NotAllowed);
}
if (result.IsLockedOut)
{
_logger.LogWarning(2, $"{model.username} قفل شدهاست.");
return BadRequest(Messages.IsLocked);
}
return BadRequest(Messages.IncorrectUsernamePassword);
}
}
}
Startup :
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddCustomServices();
}
AddCustomServices:
public static IServiceCollection AddCustomServices(this IServiceCollection services)
{
services.AddScoped<IUnitOfWork, ApplicationDbContext>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<IPrincipal>(provider =>
provider.GetService<IHttpContextAccessor>()?.HttpContext?.User ?? ClaimsPrincipal.Current);
services.AddScoped<IApplicationSignInManager, ApplicationSignInManager>();
services.AddScoped<SignInManager<User>, ApplicationSignInManager>();
services.AddScoped<IApplicationUserManager, ApplicationUserManager>();
services.AddScoped<UserManager<User>, ApplicationUserManager>();
services.AddScoped<IApplicationUserStore, ApplicationUserStore>();
services.AddScoped<UserStore<User, Role, ApplicationDbContext, int, UserClaim, UserRole, UserLogin, UserToken, RoleClaim>, ApplicationUserStore>();
return services;
}
Upvotes: 0
Views: 729
Reputation: 2311
You have not configured DbContext in your application.
Add IdentityDbContext to your application :
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions options)
: base(options)
{
}
}
And then register it in ConfigureServices :
services.AddDbContextPool<ApplicationDbContext>(opt => opt.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
And appSettings.json where ConnectionStrings are defined :
{
"ConnectionStrings": {
"DefaultConnection": "Server=Server-Name; Database=DBName; Trusted_Connection=True; MultipleActiveResultSets=True;"
}
}
Upvotes: 1