Logout.cshtml.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. #nullable disable
  4. using System;
  5. using System.Threading.Tasks;
  6. using bitforum.Models.User;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Identity;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Microsoft.AspNetCore.Mvc.RazorPages;
  11. using Microsoft.Extensions.Logging;
  12. namespace bitforum.Areas.Identity.Pages.Account
  13. {
  14. public class LogoutModel : PageModel
  15. {
  16. private readonly SignInManager<ApplicationUser> _signInManager;
  17. private readonly ILogger<LogoutModel> _logger;
  18. public LogoutModel(SignInManager<ApplicationUser> signInManager, ILogger<LogoutModel> logger)
  19. {
  20. _signInManager = signInManager;
  21. _logger = logger;
  22. }
  23. public async Task<IActionResult> OnPost(string returnUrl = null)
  24. {
  25. await _signInManager.SignOutAsync();
  26. _logger.LogInformation("User logged out.");
  27. if (returnUrl != null)
  28. {
  29. return LocalRedirect(returnUrl);
  30. }
  31. else
  32. {
  33. // This needs to be a redirect so that the browser performs a new
  34. // request and the identity for the user gets updated.
  35. return RedirectToPage();
  36. }
  37. }
  38. }
  39. }