Why is my application logging out after saving a record? - Asp.net MVC and Identity

Asked

Viewed 38 times

0

When I first created my app (Asp.net core MVC) Identity was installed automatically. All login features, user records, among others, were created automatically.

When I log in using my email, everything seems to be working 100%, but when I open a registration page and register a record and click the save button, the system is saving the record and soon after, is logging out. I don’t understand why you’re disconnecting the user... I have no idea where he’s calling the Logout action. Anyone have any suggestions?

Thank you :)

Account Controller (Created Automatically by Asp.net)

[HttpPost]
[Route("account/logout")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Logout()
{
    await _signInManager.SignOutAsync();
    _logger.LogInformation("User logged out.");
    return RedirectToAction(nameof(HomeController.Index), "Home");
}

Country Register Controller

//[Authorize]
[AllowAnonymous]
public class PaisController : BaseController
{
    private readonly IPaisAppService _paisAppService;

    public PaisController(IPaisAppService paisAppService,
                             INotificationHandler<DomainNotification> notifications) : base(notifications)
    {
        _paisAppService = paisAppService;
    }       

    [HttpGet]
    [Authorize(Policy = "CanWritePaisData")]
    [Route("pais-gerenciar/cadastrar-novo")]
    public IActionResult Create()
    {
        return View(_paisAppService.CreateNew());
    }

    [HttpPost]
    [Authorize(Policy = "CanWritePaisData")]
    [Route("pais-gerenciar/cadastrar-novo")]
    [ValidateAntiForgeryToken]
    public IActionResult Create(PaisViewModel paisViewModel)
    {
        if (!ModelState.IsValid) return View(paisViewModel);

        int paisId = _paisAppService.Register(paisViewModel);

        if (IsValidOperation())
            ViewBag.Sucesso = "País cadastrado!";

        if (paisId > 0)
        {
            ModelState.Clear();

            TempData["SucessoNew"] = ViewBag.Sucesso;

            return RedirectToAction("Edit", "Pais", new { id = paisId});
        }

        return View(paisViewModel);

    }       
}

[HttpPost]
[Authorize(Policy = "CanWritePaisData")]
[Route("pais-gerenciar/cadastrar-novo")]
[ValidateAntiForgeryToken]
public IActionResult Create(PaisViewModel paisViewModel)
{
    if (!ModelState.IsValid) return View(paisViewModel);

    int paisId = _paisAppService.Register(paisViewModel);

    if (IsValidOperation())
        ViewBag.Sucesso = "País cadastrado!";

    if (paisId > 0)
    {
        ModelState.Clear();

        TempData["SucessoNew"] = ViewBag.Sucesso;

        return RedirectToAction("Edit", "Pais", new { id = paisId});
    }

    return View(paisViewModel);

}

[HttpGet]
[Authorize(Policy = "CanWritePaisData")]
[Route("pais-gerenciar/editar-pais/{id:int}")]
public IActionResult Edit(int? id)
{
    if (id == null)
    {
        return NotFound();
    }

    var pais = _paisAppService.GetById(id.Value);

    if (pais == null)
    {
        return NotFound();
    }

    if (TempData["SucessoNew"] != null)
        ViewBag.Sucesso = TempData["SucessoNew"];

    return View(pais);
}
  • After registering, you are doing a "Redirecttoaction" for Edit parents, see Authorize and most likely you do not have permission to edit page, then it return to login.

  • Yes, after creating the record, I am doing the Redirectto Action. I edited the post.

  • After saving the system faxz logout and redirects to home/index... strange as it does not redirect to login....

  • then does a raise on Return Redirecttoaction("Edit", "Parents", new { id = paisId});, asks to redirect to another page, because the problem is at this point

  • So @itasouza, even following your suggestion, the problem persists. It seems that there is something cleaning the cookie... I have no idea...

No answers

Browser other questions tagged

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