Open Closed

How to set the project to jump directly to the login page #1335


User avatar
0
zhonggj created
  • UI type: Blazor server
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace:
  • Steps to reproduce the issue:How to set the project to jump directly to the login page

12 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Have you tried using NavigationManager in Index page?

  • User Avatar
    0
    zhonggj created

    Haven't tried, help provide a sample code.

    Like your example website, it automatically jumps to the login page.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can give it a try. I think it will be works.

    Like your example website, it automatically jumps to the login page.

    The demo website is angular. You are using Blazor Server.

  • User Avatar
    0
    zhonggj created

    I am using blazor server, and I found that your example is not enough. The effect I want is that when accessing the system homepage, if there is no login, it will automatically jump to the login page. There is no need to manually click login and then jump to the login page.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    You can inject the ICurrentUser service and use IsAuthenticated to check the user whatever is logged.

  • User Avatar
    0
    abpVAndy created

    hi

    You can give it a try. I think it will be works.

    Like your example website, it automatically jumps to the login page.

    The demo website is angular. You are using Blazor Server.

    Hi maliming,

    Where the code for Angular. I also need to do like jumping to the login page. We use angular and Identity Server Separated.

    Thx

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can try this method in Angular.

    this.authService.navigateToLogin();
    
  • User Avatar
    0
    pvaz created

    Hi Maliming,

    it was good to have an example on how to do it using mvc, I have the same need and cannot find a proper example.

    Thx

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi pvaz

    You can call ChallengeAsync in yourOnGetAsync method of IndexModel for MVC tiered application.

    public class IndexModel : MyProjectNamePageModel
    {
        public async Task OnGetAsync()
        {
            if (!CurrentUser.IsAuthenticated)
            {
                await HttpContext.ChallengeAsync("oidc");
            }
        }
    
        public async Task OnPostLoginAsync()
        {
            await HttpContext.ChallengeAsync("oidc");
        }
    }
    
  • User Avatar
    0
    pvaz created

    Hi Maliming,

    thank you for your quick reply.

    Perhaps my question I was not clear. When the user is not authenticated to redirect to /Account/Login.

    Using your example I get:

    "An unhandled exception occurred while processing the request. InvalidOperationException: No authentication handler is registered for the scheme 'oidc'. The registered schemes are: idsrv, idsrv.external, Abp.ConfirmUser, Identity.Application, Identity.External, Identity.TwoFactorRememberMe, Identity.TwoFactorUserId, Bearer, Google, Microsoft, Twitter. Did you forget to call AddAuthentication().AddSomeAuthHandler? Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, string scheme, AuthenticationProperties properties)"

    Thx

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi pvaz

    I think you can redirect user to any page.

    if (!CurrentUser.IsAuthenticated)
    {
        //redirect to /Account/Login
    }
    

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.redirect?view=aspnetcore-5.0

  • User Avatar
    0
    pvaz created

    Hi,

    solved this way using your advice:

    var redirectUrl = UriHelper.BuildAbsolute(HttpContext.Request.Scheme, HttpContext.Request.Host, "/Account/Login"); await HttpContext.ChallengeAsync(new AuthenticationProperties { RedirectUri = redirectUrl });

    return;`

Made with ❤️ on ABP v9.1.0-preview. Updated on December 12, 2024, 07:15