Logout does not redirect to Homepage after Logout in my .Net 5 Webassembly Project

Posted 3 years ago by Andreas
0

Hello Everyone,

I was not able to get my scaffolded logout method on the Server (Webassembly is hosted on Server) to redirect to the homepage (https://localhost:5001) but instead it naviagtes and stays at https://localhost:5001/authentication/logged-out where it simply statets: You are logged out.

Here is my Logout.cshtml:

@page
@model LogoutModel
@{
    ViewData["Title"] = "Log out";
}

<header>
    <h1>@ViewData["Title"]</h1>
    @{
        if (User.Identity.IsAuthenticated)
        {
            <form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Page("/", new { area = "" })" method="post">
                <button type="submit" class="nav-link btn btn-link text-dark">Click here to Logout</button>
            </form>
        }
        else
        {
            <p>You have successfully logged out of the application.</p>
        }
    }
</header>

Here is my Logout.cshtml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using XX.Shared.Models;

namespace XX.Server.Areas.Identity.Pages.Account
{
    [AllowAnonymous]
    public class LogoutModel : PageModel
    {
        private readonly SignInManager<ApplicationUser> _signInManager;
        private readonly ILogger<LogoutModel> _logger;

        public LogoutModel(SignInManager<ApplicationUser> signInManager, ILogger<LogoutModel> logger)
        {
            _signInManager = signInManager;
            _logger = logger;
        }

        public void OnGet()
        {
        }

        public async Task<IActionResult> OnPost(string returnUrl = null)
        {
            await _signInManager.SignOutAsync();
            _logger.LogInformation("User logged out.");
            if (returnUrl != null)
            {
                return LocalRedirect(returnUrl);
            }
            else
            {
                return RedirectToPage();
            }
        }
    }
}

I would appreciate any help greatly! Thank you so much, kindest blessings, Andreas

 

  • 1

    Try editing the "Authentication.razor" file in the Client project. Something like this should work okay and accomplish what you're wanting to do.

    @page "/authentication/{action}"
    @using Microsoft.AspNetCore.Components.WebAssembly.Authentication
    @inject NavigationManager navigationManager
    
    <RemoteAuthenticatorView Action="@Action">
        <LogOutSucceeded>
            @InvokeAsync(() => navigationManager.NavigateTo("/"))
        </LogOutSucceeded>
    </RemoteAuthenticatorView>
    
    @code{
        [Parameter] public string Action { get; set; }
    }
    
    Posted 3 years ago by selliott
  • 0

    Thank you so very much! That was precisely it!

    Kind blessings, Andreas

    Posted 3 years ago by Andreas
Someone is typing...

Post a Reply

You must be logged in to add a new post.
Number of online users: 1
An error has occurred. This application may no longer respond until reloaded. Reload 🗙