Пример #1
0
        public override Task OnConnected()
        {
            var applicationName = Convert.ToString(HttpContext.Current.Request.QueryString["application_name"]);

            var identity = (ClaimsIdentity)Context.User.Identity;
            var username = identity.Claims.Where(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress").FirstOrDefault().Value;;

            NotificationRepository repository = new NotificationRepository();

            NotificationUser user = repository.GetUser(username, applicationName);

            if (user != null)
            {
                NotificationUserConnection connection = new NotificationUserConnection
                {
                    UserName     = user.UserName,
                    ConnectionId = Context.ConnectionId,
                    UserAgent    = Context.Request.Headers["User-Agent"],
                    IsConnected  = true
                };

                repository.AddUserConnection(connection);
                Clients.Client(Context.ConnectionId).refreshNotification(repository.GetNotificationMessages(applicationName, username));
            }

            return(base.OnConnected());
        }
        public void AddUserConnection(NotificationUserConnection connection)
        {
            using (NotificationEntities dc = new NotificationEntities())
            {
                UserConnection userconnection = new UserConnection
                {
                    Id        = connection.ConnectionId,
                    UserName  = connection.UserName,
                    UserAgent = connection.UserAgent
                };

                dc.UserConnections.Add(userconnection);
                dc.SaveChanges();
            }
        }