JorgenV
JorgenV

Reputation: 39

Avoid user being signed out when working locally

I use the CookieAuthenticationOptions.ExpireTimeSpan to set the timespan for the user session. And it works fine - maybee too good. When the user works by filling out textboxes etc., there is no server activity, which causes that when the user press Save, he is being signed out - and looses all entered data.

Is there any way where I can update timestamp, to prevent time expires when the user works without postback?

I have some ideas:

I am using AngularJS 1.4.7 as JavaScript framework.

Upvotes: 3

Views: 72

Answers (4)

daveyfaherty
daveyfaherty

Reputation: 4613

Assuming you are using ng-model in the form, you can watch the model for changes, and ping the server when it changes.

$scope.$watch('formModel',function(){
   //ping the server
},true);

In order to avoid having a ping on every keypress or other change, you could use a timer.

var okToPing = true;    
$scope.$watch('formModel',function(){
       if(okToPing){
          okToPing = false;
          //ping the server
          setTimeout(function(){okToPing=true},10000);
    },true);

I've put together a codepen here to show how it could work: http://codepen.io/daveycakes/pen/mRewbJ

Upvotes: 1

hsyn.ozkara
hsyn.ozkara

Reputation: 127

I think you want to catch ExpireTimespan timeout and update or extend Expiretime. this link may be help you. Handle timeout and manage them..

http://markfreedman.com/handling-session-and-authentication-timeouts-in-asp-net-mvc/

Upvotes: 0

edc65
edc65

Reputation: 467

Use javascript to ping the server only when the user is in "form filling" mode. For instance you can start the ping when the user start editing and stop it when they save the data. Users probably will not leave the pc unattended when filling data in a form - and if they do, it's their fault.

Upvotes: 0

Simmetric
Simmetric

Reputation: 1671

Why not use a preprocessor directive to set an alternate expiration time if the solution is in debug mode?

var options = new CookieAuthenticationOptions {
#if DEBUG
    ExpireTimeSpan = System.Timespan.FromDays(1)
#else
    ExpireTimespan = System.Timespan.FromMinutes(20) //or your preferred timespan
#endif
}

Upvotes: 1

Related Questions