public IotHubConnectionString(IotHubConnectionStringBuilder builder) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } Audience = builder.HostName; HostName = string.IsNullOrEmpty(builder.GatewayHostName) ? builder.HostName : builder.GatewayHostName; SharedAccessKeyName = builder.SharedAccessKeyName; SharedAccessKey = builder.SharedAccessKey; SharedAccessSignature = builder.SharedAccessSignature; IotHubName = builder.IotHubName; DeviceId = builder.DeviceId; ModuleId = builder.ModuleId; HttpsEndpoint = new UriBuilder(Uri.UriSchemeHttps, HostName).Uri; AmqpEndpoint = new UriBuilder(CommonConstants.AmqpsScheme, HostName, DefaultSecurePort).Uri; if (builder.AuthenticationMethod is AuthenticationWithTokenRefresh) { TokenRefresher = (AuthenticationWithTokenRefresh)builder.AuthenticationMethod; if (Logging.IsEnabled) { Logging.Info(this, $"{nameof(IAuthenticationMethod)} is {nameof(AuthenticationWithTokenRefresh)}: {Logging.IdOf(TokenRefresher)}"); } if (Logging.IsEnabled) { Logging.Associate(this, TokenRefresher, nameof(TokenRefresher)); } Debug.Assert(TokenRefresher != null); } else if (!string.IsNullOrEmpty(SharedAccessKey)) { if (ModuleId.IsNullOrWhiteSpace()) { TokenRefresher = new DeviceAuthenticationWithSakRefresh(DeviceId, this) as AuthenticationWithTokenRefresh; if (Logging.IsEnabled) { Logging.Info(this, $"{nameof(IAuthenticationMethod)} is {nameof(DeviceAuthenticationWithSakRefresh)}: {Logging.IdOf(TokenRefresher)}"); } } else { TokenRefresher = new ModuleAuthenticationWithSakRefresh(DeviceId, ModuleId, this) as AuthenticationWithTokenRefresh; if (Logging.IsEnabled) { Logging.Info(this, $"{nameof(IAuthenticationMethod)} is {nameof(ModuleAuthenticationWithSakRefresh)}: {Logging.IdOf(TokenRefresher)}"); } } if (Logging.IsEnabled) { Logging.Associate(this, TokenRefresher, nameof(TokenRefresher)); } Debug.Assert(TokenRefresher != null); } }
public IotHubConnectionString(IotHubConnectionStringBuilder builder) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } Audience = builder.HostName; IsUsingGateway = !string.IsNullOrEmpty(builder.GatewayHostName); HostName = IsUsingGateway ? builder.GatewayHostName : builder.HostName; SharedAccessKeyName = builder.SharedAccessKeyName; SharedAccessKey = builder.SharedAccessKey; IotHubName = builder.IotHubName; DeviceId = builder.DeviceId; ModuleId = builder.ModuleId; HttpsEndpoint = new UriBuilder(Uri.UriSchemeHttps, HostName).Uri; AmqpEndpoint = new UriBuilder(CommonConstants.AmqpsScheme, HostName, DefaultSecurePort).Uri; if (builder.AuthenticationMethod is AuthenticationWithTokenRefresh authWithTokenRefresh) { TokenRefresher = authWithTokenRefresh; if (Logging.IsEnabled) { Logging.Info( this, $"{nameof(IAuthenticationMethod)} is {nameof(AuthenticationWithTokenRefresh)}: {Logging.IdOf(TokenRefresher)}"); Logging.Associate(this, TokenRefresher, nameof(TokenRefresher)); } Debug.Assert(TokenRefresher != null); } else if (!string.IsNullOrEmpty(SharedAccessKey)) { if (ModuleId.IsNullOrWhiteSpace()) { // Since the SDK creates the instance of disposable DeviceAuthenticationWithSakRefresh, the SDK needs to // dispose it once the client is disposed. TokenRefresher = new DeviceAuthenticationWithSakRefresh( DeviceId, this, builder.SasTokenTimeToLive, builder.SasTokenRenewalBuffer, disposeWithClient: true); if (Logging.IsEnabled) { Logging.Info( this, $"{nameof(IAuthenticationMethod)} is {nameof(DeviceAuthenticationWithSakRefresh)}: {Logging.IdOf(TokenRefresher)}"); } } else { // Since the SDK creates the instance of disposable ModuleAuthenticationWithSakRefresh, the SDK needs to // dispose it once the client is disposed. TokenRefresher = new ModuleAuthenticationWithSakRefresh( DeviceId, ModuleId, this, builder.SasTokenTimeToLive, builder.SasTokenRenewalBuffer, disposeWithClient: true); if (Logging.IsEnabled) { Logging.Info(this, $"{nameof(IAuthenticationMethod)} is {nameof(ModuleAuthenticationWithSakRefresh)}: {Logging.IdOf(TokenRefresher)}"); } } if (Logging.IsEnabled) { Logging.Associate(this, TokenRefresher, nameof(TokenRefresher)); } Debug.Assert(TokenRefresher != null); } // SharedAccessSignature should be set only if it is non-null and the authentication method of the device client is // not of type AuthenticationWithTokenRefresh. // Setting the sas value for an AuthenticationWithTokenRefresh authentication type will result in tokens not being renewed. // This flow can be hit if the same authentication method is always used to initialize the client; // as in, on disposal and reinitialization. This is because the value of the sas token computed is stored within the authentication method, // and on reinitialization the client is incorrectly identified as a fixed-sas-token-initialized client, // instead of being identified as a sas-token-refresh-enabled-client. else if (!string.IsNullOrWhiteSpace(builder.SharedAccessSignature)) { SharedAccessSignature = builder.SharedAccessSignature; } }