예제 #1
0
        public async Task CloseSessionAsync(int userId, string sessionId, bool banDevice = false)
        {
            Session session = await context.Sessions.FirstOrDefaultAsync(s => s.SessionId == sessionId && s.UserId == userId);

            if (session != null)
            {
                var sessions = await cache.GetListAsync <SignalRSession>(userId.ToString());

                SignalRSession signalRSession = sessions.LastOrDefault(s => s.SessionId == session.SessionId);
                if (signalRSession != null)
                {
                    await cache.RemoveFromListAsync <SignalRSession>(userId.ToString(), signalRSession);

                    await messHub.Clients.Client(signalRSession.ConnectionId).SendAsync("OnSessionClosed");
                }
                await cache.SetStringAsync("sc:" + session.SessionId, "sessionIsClosed", new DistributedCacheEntryOptions()
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(jwtOptions.Value.ExpiryMinutes)
                });

                if (banDevice)
                {
                    context.BannedDevices.Add(new BannedDevice()
                    {
                        UserId = userId, Fingerprint = session.FingerPrint
                    });
                }
                context.Sessions.Remove(session);
                await context.SaveChangesAsync();
            }
        }
예제 #2
0
 public async Task Authorize(string token, string signalRFingerprint)
 {
     if (!IsAuthorized())
     {
         var result = authServ.ValidateToken(token, true);
         if (result.IsSuccessful)
         {
             ClaimsIdentity identity    = new ClaimsIdentity(result.Principial.Identity);
             string         fingerprint = identity.FindFirst("Fingerprint").Value;
             if (fingerprint == signalRFingerprint)
             {
                 string userId    = identity.FindFirst("UserId").Value;
                 string sessionId = identity.FindFirst("SessionId").Value;
                 string ip        = Context.GetHttpContext().Connection.RemoteIpAddress.ToString();
                 Context.Items.Add("Auth", true);
                 Context.Items.Add("UserId", userId);
                 Context.Items.Add("Fingerprint", fingerprint);
                 Context.Items.Add("Ip", ip);
                 Context.Items.Add("SessionId", sessionId);
                 // await Groups.AddToGroupAsync(Context.ConnectionId, userId);
                 SignalRSession session = new SignalRSession()
                 {
                     UserId       = Int32.Parse(userId),
                     SessionId    = sessionId,
                     Fingerprint  = fingerprint,
                     Ip           = ip,
                     ConnectionId = Context.ConnectionId,
                     StartedAt    = DateTime.Now
                 };
                 await cache.AddToListAsync(userId, session);
             }
             else
             {
                 logger.LogWarning("Hub auth fail. Fingerprints are not same. Token: " + token);
                 await Clients.Caller.SendAsync("OnHubAuthFalied", "Token doesn't belong this device.");
             }
         }
         else
         {
             logger.LogWarning("Hub auth fail. Token: " + token);
             await Clients.Caller.SendAsync("OnHubAuthFalied", "Invalid token.");
         }
     }
 }