Open Closed

How to to Login page when accessing the app and after logout #1152


0
Leonardo.Willrich created
  • ABP Framework version: v4.3.0 rc.1
  • UI type:Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes / no
  • Exception message and stack trace:
  • Steps to reproduce the issue:

Hi,

Nowadays, when accessing the app it redirect for the home page where I am not using for nothing in my application. So, the user has to click in login to access the app. If he logout, I want to redirect to the login page. In other word, I don't want to use that Home page where there is no user logged. I want to go from Login to App (Host side or Tenant site), and then when logout or the session is expired redirecting to Login page, similar as AspNetZero does.

Is that possible? If don't, how can I require that as a framework feature?


7 Answer(s)
  • 0
    liangshiwei created
    Support Team

    Hi,

    Try

    public partial class Index
    {
        [Inject]
        public NavigationManager NavigationManager { get; set; }
    
        protected override void OnInitialized()
        {
            if (!CurrentUser.IsAuthenticated)
            {
                NavigationManager.NavigateTo("/authentication/login");
            }
        }
    }
    
  • 0
    Leonardo.Willrich created

    @liangshiwei

    Perfect! I think that does the trick.

  • 0
    Leonardo.Willrich created

    Well, it worked partially. When the user logs out, the application is redirected for a different page. I think I should do the same, I just need to know where I can find this page:

  • 0
    Leonardo.Willrich created

    Also, it seems that the CurrentUser.IsAuthenticated is not working properly. After login, the user is redirected to the Index page, but it get into a loop redirecting to the same page over and over again.

    I've added a console message to debug it:

    protected override void OnInitialized()
            {
                if (!CurrentUser.IsAuthenticated)
                {
                    Console.WriteLine("Redirecting to Login");
                    NavigationManager.NavigateTo("/authentication/login");
                }
            }
    

    My console:

  • 0
    liangshiwei created
    Support Team

    Hi,

    I find a better way,

    [Authorize]
    public partial class Index
    {
       
    }
    

    Pages/Authentication.razor

    @using Microsoft.AspNetCore.Components.WebAssembly.Authentication
    @using Volo.Abp.DependencyInjection
    @inherits  Volo.Abp.AspNetCore.Components.WebAssembly.LeptonTheme.Pages.Authentication
    @inject NavigationManager _navigationManager;
    @attribute [ExposeServices(typeof(Volo.Abp.AspNetCore.Components.WebAssembly.LeptonTheme.Pages.Authentication))]
    @attribute  [Dependency(ReplaceServices = true)]
    
    <Card>
        <CardBody>
            <RemoteAuthenticatorView Action="@Action">
                <LoggingIn>
                    <LoadingIndicator/>
                </LoggingIn>
                <CompletingLoggingIn>
                    <LoadingIndicator/>
                </CompletingLoggingIn>
                <LogOut>
                    <LoadingIndicator/>
                </LogOut>
                <CompletingLogOut>
                    <LoadingIndicator/>
                </CompletingLogOut>
                <LogOutSucceeded>
                    @{
                        _navigationManager.NavigateTo("/authentication/login");
                    }
                </LogOutSucceeded>
            </RemoteAuthenticatorView>
        </CardBody>
    </Card>
    
    
  • 0
    Leonardo.Willrich created

    I am still not happy with the solution. It is taking around 8 seconds for the user to load the page, click on login and then be able to type his credential. Is there another option to open straight away the Login page instead of just redirecting it?

  • 0
    liangshiwei created
    Support Team

    Hi,

    You can try put the MyLoggedOutModel.cs file to your .HttpApi.Host or .IdentityServer project

    MyLoggedOutModel.cs

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(LoggedOutModel))]
    public class MyLoggedOutModel : LoggedOutModel
    {
        public override Task<IActionResult> OnGetAsync()
        {
            var blazorUrl = "https://<blazor>"; // front-end url
            if (PostLogoutRedirectUri.StartsWith(blazorUrl))
            {
                PostLogoutRedirectUri = $"https://<auth-server>/account/login?returnUrl={blazorUrl}";
                SignOutIframeUrl = null;
            }
    
            return base.OnGetAsync();
        }
    }