This article demonstrates MVC using Angular Idle with a UI Bootstrap. This article will show you how to get your application to report if a user is idle.

Angular Idle

Angular Idle can use an Angular module to detect and respond to idle users. We can almost maintain the session on the client side.

Follow the steps given below and we can use an Angular Idle in AngularJS in MVC.

  • Create MVC project.
  • Configure Angular Idle.
  • Work with Angular Idle.

Create an MVC Project

Open Visual Studio 2015.
MVC

Go to New menu >Click New and then Project. Now, it will open a New Project Window.
MVC

You can select ASP.NET Web Application on Framework 4.6. Enter the name of the project in the Solution name text box, then click OK.

MVC

One more Window should appear. Select the MVC template in this popup and click Ok. Now, you can start.

Configure Angular Idle

We will download the idle plugin from the following sources:

Open the _Layout.cshtml and refer the.js file from downloaded folder to this view page:

<script src="~/Plugin/angular/angular.min.js"></script>
<script src="~/Plugin/angular-ui-router/release/angular-ui-router.min.js"></script>
<script src="~/Plugin/angular-idle/angular-idle.js"></script>
<script src="~/Plugin/angular-bootstrap/ui-bootstrap-tpls.min.js"></script>

Link to My File

<script src="~/App/App.module.js"></script>
<script src="~/App/App.config.js"></script>
<script src="~/App/CarController.js"></script>
<script src="~/App/LoginConttroller.js"></script>

Angular Module

You will need to include the module as a dependency of your application.

var uiroute = angular
.module('uiroute', ['ui.router', 'ngIdle', 'ui.bootstrap'])

Angular Config

You should also set your options, using the KeepaliveProvider, IdleProvider in your Angular config file.

uiroute.config(['KeepaliveProvider', 'IdleProvider', function(KeepaliveProvider, IdleProvider) {
    IdleProvider.idle(10);
    IdleProvider.timeout(10);
    KeepaliveProvider.interval(10);
}]);

Angular Controller

You can set what action will be performed on the user idle times. With the below code, I will be shown a warning message and be notified of the session logout.

function closeModals() {
    if ($scope.warning) {
        $scope.warning.close();
        $scope.warning = null;
    }
    if ($scope.timedout) {
        $scope.timedout.close();
        $scope.timedout = null;
    }
}
$scope.$on('IdleStart', function() {
    closeModals();
    $scope.warning = $uibModal.open({
        templateUrl: 'warning-dialog.html',
        windowClass: 'modal-danger'
    });
});
$scope.$on('IdleEnd', function() {
   // closeModals();
});
$scope.$on('IdleTimeout', function() {
    //closeModals();
    $scope.timedout = $uibModal.open({
        templateUrl: 'timedout-dialog.html',
        windowClass: 'modal-danger'
    });
});

Angular Run

After configuring the angular.idle.js, you must initiate the function in your Angular Run function.

uiroute.run(['Idle', function(Idle) {
    Idle.watch();
}]);

HTML Code

Set your HTML with the warning dialog and timeout dialog that is given below.

<script type="text/ng-template" id="warning-dialog.html">
    
        

Your are Idle.

    
    
        You'll be logged out in {{countdown}} second(s).
    
</script>
<script type="text/ng-template" id="timedout-dialog.html">
    
        

You have Timed Out!

    
    
        

You were idle too long. you'll be reset.

    
    
</script>