Internal Server Error - Iauthentication

Asked

Viewed 27 times

0

I am with the problem presented below and do not know how to solve, the system is complaining that can not access my class Iauthentication.

follows below the error presented.

Tela do erro na compilação da view Register

Below my controller code

using System.Linq;
using System.Threading.Tasks;
using MeuConsultorio.Domain.Account;
using MeuConsultorio.Web.ViewsModels.Account;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace MeuConsultorio.Web.Controllers
{
    public class AccountController : Controller
    {
        private readonly IAuthentication _authentication;
        private readonly IManager _manager;
        private readonly ILogger _logger;


        public AccountController(IAuthentication authentication,
            IManager manager,
            ILogger<AccountController> logger
            )
        {

            _authentication = authentication;
            _manager = manager;
            _logger = logger;

        }
        // GET: Account
        public IActionResult Index()
        {
            var users = _manager.ListAll();
            var userViewModel = users.Select(u => new UserViewModel { Id = u.Id, Email = u.Email });

            return View();
        }

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

        // GET: Account/Create
        public IActionResult Register()
        {
            return View();
        }

        // POST: Account/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Register(RegisterViewModel viewModel)
        {

            await _manager.CreateAsync(viewModel.Email, viewModel.Password, viewModel.Role);

            return Ok();

            //try
            //{
            //    // TODO: Add insert logic here
            //    await _manager.CreateAsync(viewModel.Email, viewModel.Password, viewModel.Role);

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

        public IActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Login(LoginViewModel login)
        {
            var result = await _authentication.Authenticate(login.UserName, login.Password);

            if (result)
                return Redirect("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();
    }
}

Authentication class 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();
        }
    }
}
  • I ended up solving my problems after a lot of effort.

1 answer

0


I managed to solve my problems after much 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);
        }

Browser other questions tagged

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