public virtual Task DeleteAsync(ClientSession clientSession)
        {
            if (clientSession == null)
            throw new ArgumentNullException("clientSession");

              return Task.Factory.StartNew(() =>
              {
            sessions.ClientSessions.Remove(sessions.ClientSessions.Where(x => x.Id==clientSession.Id).SingleOrDefault<ClientSession>());
            //IDbConnection connection = CurrentContext.OpenConnection(CurrentContext.CurrentTransaction);
            //connection.Execute("delete from auth_ClientSessions where Id = @Id", new { clientSession.Id }, CurrentContext.CurrentTransaction);
              });
        }
 public async Task<ActionResult> ConfirmLogin(string sessionID, string returnUrl)
 {
   WorkManager.BeginWork();
   ClientSession _session = new ClientSession();
   _session.ClientSessionID = new Guid(sessionID);
   _session.LocalSessionID = Session.SessionID;
   await WorkManager.SessionManager.CreateAsync(_session);
   if (Request.IsAuthenticated == true)
   {
     Login _login = await WorkManager.LoginManager.FindOpenBySessionAsync(Session.SessionID);
     if(_login==null)
     {
       User user = UserManager.FindById(new Guid(User.Identity.GetUserId()));
       if(user==null)
       {
         throw new Exception("User Record Not Found");
       }
       else
       {
         _login = await WorkManager.LoginManager.AddLoginRecordAsync(user, Session.SessionID);
       }
     }
     await WorkManager.SessionManager.AssignLoginToSessions(Session.SessionID, _login);
     WorkManager.CommitWork();
     if (returnUrl == null || returnUrl == "")
     {
       return RedirectToAction("Manage", "Account");
     }
     else
     {
       return Redirect(returnUrl);
     }
     //
   }
   else
   {
     Login _openLogin = await WorkManager.LoginManager.FindOpenBySessionAsync(Session.SessionID);
     if(_openLogin!=null)
     {
       await WorkManager.LoginManager.LogoutAsync(_openLogin);
     }
     WorkManager.CommitWork();
     if (returnUrl == "")
     {
       return RedirectToAction("Login");
     }
     else
     {
       return Redirect(returnUrl);
     }
   }
 }
 public virtual Task CreateAsync(ClientSession clientSession)
 {
     if (clientSession == null)
     throw new ArgumentNullException("clientSession");
       var owner = this.FindByClientAsync(clientSession.ClientSessionID);
       if ((owner == null) || (owner.Result == null))
       {
     return Task.Factory.StartNew(() =>
     {
       clientSession.Id = Guid.NewGuid();
       sessions.ClientSessions.Add(clientSession);
       //IDbConnection connection = CurrentContext.OpenConnection(CurrentContext.CurrentTransaction);
       //connection.Execute("insert into auth_ClientSessions(Id, LocalSessionID, ClientSessionID, LoginID) values(@Id, @LocalSessionID, @ClientSessionID, @LoginID)", clientSession, CurrentContext.CurrentTransaction);
     });
       }
       else
       {
     clientSession.Id = owner.Result.Id;
     this.UpdateAsync(clientSession);
     return Task.FromResult(0);
       }
 }
        public virtual Task UpdateAsync(ClientSession clientSession)
        {
            if (clientSession == null)
            throw new ArgumentNullException("user");
              if(clientSession.LoginID == Guid.Empty)
            clientSession.LoginID = null;

              var currentSession = FindByIdAsync(clientSession.Id);

              return Task.Factory.StartNew(() =>
              {
            if((currentSession!=null)&&(currentSession.Result!=null))
            {
              currentSession.Result.LocalSessionID=clientSession.LocalSessionID;
              currentSession.Result.ClientSessionID = clientSession.ClientSessionID;
              currentSession.Result.LoginID = clientSession.LoginID;
            }
            //IDbConnection connection = CurrentContext.OpenConnection(CurrentContext.CurrentTransaction);
            //connection.Execute("update auth_ClientSessions set LocalSessionID=@LocalSessionID, ClientSessionID=@ClientSessionID, LoginID=@LoginID where ID = @ID", clientSession, CurrentContext.CurrentTransaction);
              });
        }