Internal Server Error - IAuthentication

0

I have the problem presented below and I do not know how to solve it, the system is complaining that it can not access my IAuthentication class.

The following error appears below.

Belowmycontroller'scode

usingSystem.Linq;usingSystem.Threading.Tasks;usingMeuConsultorio.Domain.Account;usingMeuConsultorio.Web.ViewsModels.Account;usingMicrosoft.AspNetCore.Http;usingMicrosoft.AspNetCore.Mvc;usingMicrosoft.Extensions.Logging;namespaceMeuConsultorio.Web.Controllers{publicclassAccountController:Controller{privatereadonlyIAuthentication_authentication;privatereadonlyIManager_manager;privatereadonlyILogger_logger;publicAccountController(IAuthenticationauthentication,IManagermanager,ILogger<AccountController>logger){_authentication=authentication;_manager=manager;_logger=logger;}//GET:AccountpublicIActionResultIndex(){varusers=_manager.ListAll();varuserViewModel=users.Select(u=>newUserViewModel{Id=u.Id,Email=u.Email});returnView();}//GET:Account/Details/5publicActionResultDetails(intid){returnView();}//GET:Account/CreatepublicIActionResultRegister(){returnView();}//POST:Account/Create[HttpPost][ValidateAntiForgeryToken]publicasyncTask<IActionResult>Register(RegisterViewModelviewModel){await_manager.CreateAsync(viewModel.Email,viewModel.Password,viewModel.Role);returnOk();//try//{////TODO:Addinsertlogichere//await_manager.CreateAsync(viewModel.Email,viewModel.Password,viewModel.Role);//returnRedirectToAction(nameof(Index));//}//catch//{//returnView();//}}publicIActionResultLogin(){returnView();}[HttpPost]publicasyncTask<IActionResult>Login(LoginViewModellogin){varresult=await_authentication.Authenticate(login.UserName,login.Password);if(result)returnRedirect("index");
            else
            {
                ModelState.AddModelError(string.Empty, "Login inválido");
                return View(login);
            }
        }

        // GET: Account/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Account/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: Account/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Account/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}

And below my IAuthentication code

using System.Threading.Tasks;

namespace MeuConsultorio.Domain.Account
{
    public interface IAuthentication
    {
        Task<bool> Authenticate(string email, string password);
        Task Logout();
    }
}

Class Authentication where my interface is consumed.

using MeuConsultorio.Domain.Account;
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;

namespace MeuConsultorio.Data.Identity
{
    public class Authentication : IAuthentication
    {
        private readonly SignInManager<ApplicationUser> _signInManager;

        public Authentication(SignInManager<ApplicationUser> signInManager)
        {
            _signInManager = signInManager;
        }

        public async Task<bool> Authenticate(string email, string password)
        {
            var result = await _signInManager.PasswordSignInAsync(email, password, false, lockoutOnFailure: false);

            return result.Succeeded;
        }

        public async Task Logout()
        {
            await _signInManager.SignOutAsync();
        }
    }
}
    
asked by anonymous 22.09.2018 / 16:46

1 answer

0

I was able to solve my problems after a lot of effort. The problem was that I needed to reference my dependency injection in the Startup class.

public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.

                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            Bootstrap.Configure(services, Configuration.GetConnectionString("ScheduleDB"));


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }
    
24.09.2018 / 02:16