Мне нужно создать логин через web api, но при вводе этого URL https://localhost:44366/api/login
покажите мне эту ошибку:
InvalidOperationException: невозможно разрешить службу для типа 'Microsoft.EntityFrameworkCore.DbContextOptions`1[IRI.DataLayer.Context.ApplicationDbContext] 'при попытке активировать' IRI.DataLayer.Context.ApplicationDbContext '.
, а затем введите URL https://localhost:44366/api/login/Authenticate
, покажите мне эту ошибку
Эта страница локального хоста не может быть найдена Веб-страница не найдена для веб-адреса: https://localhost:44366/api/login/Authenticate ОШИБКА HTTP 404
в чем проблема?Как я могу решить эту проблему?
Мой код =>
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);
}
}
}
Запуск:
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;
}