Base solution for your next web application
Open Closed

Stop to redirect the login page #6733


User avatar
0
kalidarscope created

I have to access the url without login, but it is redirected to login page. I wolud like to stop the redirection to login the page for my scenario. Could you please assist me


14 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team
    • What is your product version?
    • What is your product type (Angular or MVC)?
    • What is product framework type (.net framework or .net core)?
  • User Avatar
    0
    kalidarscope created

    ASP.NET CORE and Angular

  • User Avatar
    0
    maliming created
    Support Team

    You can try to modify it in auth-route-guard. https://github.com/aspnetzero/aspnet-zero-core/blob/e9517e1e64cc815f35290d242ec59840ea487eaa/angular/src/app/shared/common/auth/auth-route-guard.ts

  • User Avatar
    0
    kalidarscope created

    I have used below code but it was not working.

    if (_.includes(state.url, 'asap-sso')) {
                this._router.navigate(['app/main/asap-sso', 66]);
                return true;
            }
    
  • User Avatar
    0
    maliming created
    Support Team

    please show your full code of auth-route-guard.ts

  • User Avatar
    0
    maliming created
    Support Team

    please show your full code of auth-route-guard.ts

  • User Avatar
    0
    kalidarscope created
    import { PermissionCheckerService } from '@abp/auth/permission-checker.service';
    import { Injectable } from '@angular/core';
    import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, CanLoad, Router, RouterStateSnapshot } from '@angular/router';
    import { AppSessionService } from '@shared/common/session/app-session.service';
    import { UrlHelper } from '@shared/helpers/UrlHelper';
    import { Data, Route } from '@node_modules/@angular/router/src/config';
    import { Observable } from '@node_modules/rxjs/internal/Observable';
    import * as _ from 'lodash';
    
    @Injectable()
    export class AppRouteGuard implements CanActivate, CanActivateChild, CanLoad {
    
        constructor(
            private _permissionChecker: PermissionCheckerService,
            private _router: Router,
            private _sessionService: AppSessionService
        ) { }
    
        canActivateInternal(data: Data, state: RouterStateSnapshot): boolean {
            debugger;
    
            if (_.includes(state.url, 'asap-sso')) {
                this._router.navigate(['app/main/asap-sso', 66]);
                return true;
            }
    
            if (UrlHelper.isInstallUrl(location.href)) {
                return true;
            }
    
            if (!this._sessionService.user) {
                this._router.navigate(['/account/login']);
                return false;
            }
    
            if (!data || !data['permission']) {
                return true;
            }
    
            if (this._permissionChecker.isGranted(data['permission'])) {
                return true;
            }
    
            this._router.navigate([this.selectBestRoute()]);
            return false;
        }
    
        canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
            return this.canActivateInternal(route.data, state);
        }
    
        canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
            return this.canActivate(route, state);
        }
    
        canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
            return this.canActivateInternal(route.data, null);
        }
    
        selectBestRoute(): string {
            debugger;
            if (!this._sessionService.user) {
                return '/account/login';
            }
    
            if (this._permissionChecker.isGranted('Pages.Administration.Host.Dashboard')) {
                //return '/app/admin/hostDashboard';            
                return '/app/admin/users';            
            }
    
            if (this._permissionChecker.isGranted('Pages.Tenant.Dashboard')) {
                return '/app/main/dashboard';
            }
    
            if (this._permissionChecker.isGranted('Pages.Tenants')) {
                return '/app/admin/tenants';
            }
    
            if (this._permissionChecker.isGranted('Pages.Administration.Users')) {
                return '/app/admin/users';
            }
    
            return '/app/notifications';        
        }
    }
    
  • User Avatar
    0
    maliming created
    Support Team

    Try modifying /account/login to /app/main/asap-sso https://github.com/aspnetzero/aspnet-zero-core/blob/e9517e1e64cc815f35290d242ec59840ea487eaa/angular/src/app/shared/common/auth/auth-route-guard.ts#L24

  • User Avatar
    0
    kalidarscope created

    i have tried as you mentioned. But it is looping infinite time. The browser is freezing. The code is below:

    canActivateInternal(data: Data, state: RouterStateSnapshot): boolean {
            debugger;
            //if (state != null) {
            //    if (_.includes(state.url, 'asap-sso')) {
            //        this._router.navigate(['app/main/asap-sso', 66]);
            //        return true;
            //    }
            //}
    
            if (UrlHelper.isInstallUrl(location.href)) {
                return true;
            }
    
            if (!this._sessionService.user) {
                //this._router.navigate(['/account/login']);
                this._router.navigate(['app/main/asap-sso']);
                return false;
            }
    
            if (!data || !data['permission']) {
                return true;
            }
    
            if (this._permissionChecker.isGranted(data['permission'])) {
                return true;
            }
    
            this._router.navigate([this.selectBestRoute()]);
            return false;
        }
    
  • User Avatar
    0
    maliming created
    Support Team

    You can consider moving the component asap-sso to the Account module or adding the following special judgment.

    if (state != null) {
                if (_.includes(state.url, 'asap-sso')) {
                    // Do not navigate.
                    //this._router.navigate(['app/main/asap-sso', 66]);
                   return true;
                }
            }
    
    canActivateInternal(data: Data, state: RouterStateSnapshot): boolean {
            debugger;
            if (state != null) {
                if (_.includes(state.url, 'asap-sso')) {
                    //this._router.navigate(['app/main/asap-sso', 66]);
                   return true;
                }
            }
    
            if (UrlHelper.isInstallUrl(location.href)) {
                return true;
            }
    
            if (!this._sessionService.user) {
                //this._router.navigate(['/account/login']);
                this._router.navigate(['app/main/asap-sso']);
                return false;
            }
    
            if (!data || !data['permission']) {
                return true;
            }
    
            if (this._permissionChecker.isGranted(data['permission'])) {
                return true;
            }
    
            this._router.navigate([this.selectBestRoute()]);
            return false;
        }
        ```
    
  • User Avatar
    0
    kalidarscope created

    Thanks @maliming. It is working now. Can you pls tell me why we not navigate.

  • User Avatar
    0
    maliming created
    Support Team

    If you call navigate, it will be intercepted again by AppRouteGuard. Infinite loop...

  • User Avatar
    0
    SRTMDEV created

    @maliming,

    you solution not worked for me

  • User Avatar
    0
    maliming created
    Support Team

    @SRTMDEV Can you create a question separately and describe in detail the problem you are experiencing?