示例#1
0
        /// <summary>
        /// The reason this is not in the constructor is solely for the purpose of exception handling.
        /// If you leave this in the controller and someone who is not authenticated calls the API you will not get a tenantId not found error.
        /// The error will be ugly and be hard to figure out you are not authorized.
        /// This way if the all methods have the ClaimsAuthorize attribute on them they will first be authenticated if not get a nice error message of not authorized.
        /// </summary>
        /// <param name="equipmentId">The equipment identifier.</param>
        /// <param name="sessionId">The session identifier.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">No Tenant Id Found.</exception>
        private ConnectionInfo Setup(int equipmentId, string sessionId)
        {
            //Get the current claims principal
            var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;
            var tenant   = identity.Claims.Where(c => c.Type == ClaimsConstants.TenantIdClaimType).Select(c => c.Value).SingleOrDefault();

            _oauth2AuthenticationSettings.Username   = identity.Claims.Where(c => c.Type == ClaimsConstants.UserNameWithoutTenant).Select(c => c.Value).SingleOrDefault();
            _oauth2AuthenticationSettings.TenantName = identity.Claims.Where(c => c.Type == ClaimsConstants.TenantNameClaimType).Select(c => c.Value).SingleOrDefault();

            if (string.IsNullOrEmpty(tenant))
            {
                throw new Exception("No Tenant Id Found.");
            }

            _tenantId = Guid.Parse(tenant);

            if (string.IsNullOrEmpty(sessionId))
            {
                sessionId = Guid.NewGuid().ToString();
            }

            //Cache Service in memory
            var memoryCachingService = CachingServiceFactory();

            return(memoryCachingService.FetchAndCache(sessionId,
                                                      //Tells caching service how to cache and retrieve.
                                                      delegate
            {
                var equipmentConnectionSetting =
                    EquipmentConnectionSettingsService.RetrieveProvisioningEquipmentSettings(equipmentId,
                                                                                             _oauth2AuthenticationSettings).EquipmentConnectionSettings;

                if (memoryCachingService.Count <ConnectionInfo>() >= equipmentConnectionSetting.MaxConcurrentConnections)
                {
                    throw new Exception("No TcpIp connection available, max concurrent connections: " + equipmentConnectionSetting.MaxConcurrentConnections);
                }

                _logger.WriteLogEntry(new List <object> {
                    equipmentConnectionSetting
                }, "Connection created in cache.", LogLevelType.Info);

                return new ConnectionInfo
                {
                    ConnectionManagerService = new ConnectionManagerService(equipmentConnectionSetting, _logger),
                    SessionId = sessionId
                };
            },
                                                      _connectionCacheTimeSpan
                                                      ));
        }
示例#2
0
        /// <summary>
        /// The reason this is not in the constructor is solely for the purpose of exception handling.
        /// If you leave this in the controller and someone who is not authenticated calls the API you will not get a tenantId not found error.
        /// The error will be ugly and be hard to figure out you are not authorized.
        /// This way if the all methods have the ClaimsAuthorize attribute on them they will first be authenticated if not get a nice error message of not authorized.
        /// </summary>
        /// <param name="equipmentId">The equipment identifier.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">No Tenant Id Found.</exception>
        private EquipmentConnectionSetting Setup(int equipmentId)
        {
            //Get the current claims principal
            var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;
            var tenant   = identity.Claims.Where(c => c.Type == ClaimsConstants.TenantIdClaimType).Select(c => c.Value).SingleOrDefault();

            _oauth2AuthenticationSettings.Username   = identity.Claims.Where(c => c.Type == ClaimsConstants.UserNameWithoutTenant).Select(c => c.Value).SingleOrDefault();
            _oauth2AuthenticationSettings.TenantName = identity.Claims.Where(c => c.Type == ClaimsConstants.TenantNameClaimType).Select(c => c.Value).SingleOrDefault();

            if (string.IsNullOrEmpty(tenant))
            {
                throw new Exception("No Tenant Id Found.");
            }

            _tenantId = Guid.Parse(tenant);
            return(EquipmentConnectionSettingsService.RetrieveProvisioningEquipmentSettings(equipmentId, _oauth2AuthenticationSettings).EquipmentConnectionSettings);
        }