コード例 #1
0
 internal void Server_ClientConnectionValidator(object sender, MqttServerClientConnectionValidatorEventArgs e)
 {
     using (var _dbContextcv = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>())
     {
         MqttConnectionValidatorContext obj = e.Context;
         Uri uri = new Uri("mqtt://" + obj.Endpoint);
         if (uri.IsLoopback && !string.IsNullOrEmpty(e.Context.ClientId) && e.Context.ClientId == _mcsetting.MqttBroker && !string.IsNullOrEmpty(e.Context.Username) && e.Context.Username == _mcsetting.UserName && e.Context.Password == _mcsetting.Password)
         {
             obj.ReasonCode = MQTTnet.Protocol.MqttConnectReasonCode.Success;
         }
         else
         {
             _logger.LogInformation($"ClientId={obj.ClientId},Endpoint={obj.Endpoint},Username={obj.Username},Password={obj.Password},WillMessage={obj.WillMessage?.ConvertPayloadToString()}");
             var mcr = _dbContextcv.DeviceIdentities.Include(d => d.Device).FirstOrDefault(mc =>
                                                                                           (mc.IdentityType == IdentityType.AccessToken && mc.IdentityId == obj.Username) ||
                                                                                           (mc.IdentityType == IdentityType.DevicePassword && mc.IdentityId == obj.Username && mc.IdentityValue == obj.Password));
             if (mcr != null)
             {
                 try
                 {
                     var device = mcr.Device;
                     if (!Devices.ContainsKey(e.Context.ClientId))
                     {
                         Devices.Add(e.Context.ClientId, device);
                     }
                 }
                 catch (Exception ex)
                 {
                     _logger.LogError(ex, "ConnectionRefusedServerUnavailable {0}", ex.Message);
                     obj.ReasonCode = MQTTnet.Protocol.MqttConnectReasonCode.ServerUnavailable;
                 }
             }
             else
             {
                 obj.ReasonCode = MQTTnet.Protocol.MqttConnectReasonCode.BadUserNameOrPassword;
                 _logger.LogInformation($"Bad username or password {obj.Username},connection {obj.Endpoint} refused");
             }
         }
     }
 }
コード例 #2
0
 internal void Server_ClientConnectionValidator(object sender, MqttServerClientConnectionValidatorEventArgs e)
 {
     try
     {
         using (var scope = _scopeFactor.CreateScope())
             using (var _dbContextcv = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>())
             {
                 MqttConnectionValidatorContext obj = e.Context;
                 Uri uri = new Uri("mqtt://" + obj.Endpoint);
                 if (uri.IsLoopback && !string.IsNullOrEmpty(e.Context.ClientId) && e.Context.ClientId == _mcsetting.MqttBroker && !string.IsNullOrEmpty(e.Context.Username) && e.Context.Username == _mcsetting.UserName && e.Context.Password == _mcsetting.Password)
                 {
                     e.Context.ReasonCode = MQTTnet.Protocol.MqttConnectReasonCode.Success;
                 }
                 else
                 {
                     _logger.LogInformation($"ClientId={obj.ClientId},Endpoint={obj.Endpoint},Username={obj.Username},Password={obj.Password},WillMessage={obj.WillMessage?.ConvertPayloadToString()}");
                     var mcr = _dbContextcv.DeviceIdentities.Include(d => d.Device).FirstOrDefault(mc =>
                                                                                                   (mc.IdentityType == IdentityType.AccessToken && mc.IdentityId == obj.Username) ||
                                                                                                   (mc.IdentityType == IdentityType.DevicePassword && mc.IdentityId == obj.Username && mc.IdentityValue == obj.Password));
                     if (mcr != null)
                     {
                         try
                         {
                             var device = mcr.Device;
                             e.Context.SessionItems.TryAdd(nameof(Device), device);
                             e.Context.ReasonCode = MQTTnet.Protocol.MqttConnectReasonCode.Success;
                             _logger.LogInformation($"Device {device.Name}({device.Id}) is online !username is {obj.Username} and  is endpoint{obj.Endpoint}");
                         }
                         catch (Exception ex)
                         {
                             _logger.LogError(ex, "ConnectionRefusedServerUnavailable {0}", ex.Message);
                             e.Context.ReasonCode = MQTTnet.Protocol.MqttConnectReasonCode.ServerUnavailable;
                         }
                     }
                     else if (_dbContextcv.AuthorizedKeys.Any(ak => ak.AuthToken == obj.Password))
                     {
                         var ak = _dbContextcv.AuthorizedKeys.Include(ak => ak.Customer).Include(ak => ak.Tenant).Include(ak => ak.Devices).FirstOrDefault(ak => ak.AuthToken == obj.Password);
                         if (ak != null && !ak.Devices.Any(dev => dev.Name == obj.Username))
                         {
                             var devvalue = new Device()
                             {
                                 Name = obj.Username, DeviceType = DeviceType.Device, Timeout = 300, LastActive = DateTime.Now
                             };
                             devvalue.Tenant   = ak.Tenant;
                             devvalue.Customer = ak.Customer;
                             _dbContextcv.Device.Add(devvalue);
                             ak.Devices.Add(devvalue);
                             _dbContextcv.AfterCreateDevice(devvalue, obj.Username, obj.Password);
                             _dbContextcv.SaveChanges();
                         }
                         var mcp = _dbContextcv.DeviceIdentities.Include(d => d.Device).FirstOrDefault(mc => mc.IdentityType == IdentityType.DevicePassword && mc.IdentityId == obj.Username && mc.IdentityValue == obj.Password);
                         if (mcp != null)
                         {
                             e.Context.SessionItems.TryAdd(nameof(Device), mcp.Device);
                             e.Context.ReasonCode = MQTTnet.Protocol.MqttConnectReasonCode.Success;
                             _logger.LogInformation($"Device {mcp.Device.Name}({mcp.Device.Id}) is online !username is {obj.Username} and  is endpoint{obj.Endpoint}");
                         }
                         else
                         {
                             e.Context.ReasonCode = MQTTnet.Protocol.MqttConnectReasonCode.BadUserNameOrPassword;
                             _logger.LogInformation($"Bad username or  password/AuthToken {obj.Username},connection {obj.Endpoint} refused");
                         }
                     }
                     else
                     {
                         e.Context.ReasonCode = MQTTnet.Protocol.MqttConnectReasonCode.BadUserNameOrPassword;
                         _logger.LogInformation($"Bad username or password {obj.Username},connection {obj.Endpoint} refused");
                     }
                 }
             }
     }
     catch (Exception ex)
     {
         e.Context.ReasonCode   = MQTTnet.Protocol.MqttConnectReasonCode.ImplementationSpecificError;
         e.Context.ReasonString = ex.Message;
         _logger.LogError(ex, "ConnectionRefusedServerUnavailable2222222222222 {0}", ex.Message);
     }
 }
 public Task HandleClientConnectionValidatorAsync(MqttServerClientConnectionValidatorEventArgs eventArgs)
 {
     return(_handler(eventArgs));
 }