Logout.cshtml.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 Microsoft.AspNetCore.Authorization;
  7. using Microsoft.AspNetCore.Identity;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.AspNetCore.Mvc.RazorPages;
  10. using Microsoft.Extensions.Logging;
  11. namespace bitforum.Areas.Identity.Pages.Account
  12. {
  13. public class LogoutModel : PageModel
  14. {
  15. private readonly SignInManager<IdentityUser> _signInManager;
  16. private readonly ILogger<LogoutModel> _logger;
  17. public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger)
  18. {
  19. _signInManager = signInManager;
  20. _logger = logger;
  21. }
  22. public async Task<IActionResult> OnPost(string returnUrl = null)
  23. {
  24. await _signInManager.SignOutAsync();
  25. _logger.LogInformation("User logged out.");
  26. if (returnUrl != null)
  27. {
  28. return LocalRedirect(returnUrl);
  29. }
  30. else
  31. {
  32. // This needs to be a redirect so that the browser performs a new
  33. // request and the identity for the user gets updated.
  34. return RedirectToPage();
  35. }
  36. }
  37. }
  38. }