static async Task <IClientCredentials> GetClientCredentials(string iotHubHostName, string deviceId, string userName, string token, bool isCertAuthAllowed = false, string productInfo = "") { var authenticator = Mock.Of <IAuthenticator>(a => a.AuthenticateAsync(It.IsAny <IClientCredentials>()) == Task.FromResult(true)); var factory = new ClientCredentialsFactory(iotHubHostName, productInfo); var sasTokenIdentityProvider = new DeviceIdentityProvider(authenticator, factory, isCertAuthAllowed); ProtocolGateway.Identity.IDeviceIdentity deviceIdentity = await sasTokenIdentityProvider.GetAsync(deviceId, userName, token, null); Assert.NotNull(deviceIdentity); IClientCredentials clientCredentials = (deviceIdentity as ProtocolGatewayIdentity)?.ClientCredentials; return(clientCredentials); }
static async Task <IClientCredentials> GetClientCredentials(string iotHubHostName, string deviceId, string userName, string token, bool isCertAuthAllowed = false, string productInfo = "", X509Certificate2 certificate = null, IList <X509Certificate2> chain = null) { var authenticator = Mock.Of <IAuthenticator>(a => a.AuthenticateAsync(It.IsAny <IClientCredentials>()) == Task.FromResult(true)); var factory = new ClientCredentialsFactory(new IdentityProvider(iotHubHostName), productInfo); var credentialIdentityProvider = new DeviceIdentityProvider(authenticator, factory, isCertAuthAllowed); if ((certificate != null) && (chain != null)) { credentialIdentityProvider.RemoteCertificateValidationCallback(certificate, chain); } ProtocolGateway.Identity.IDeviceIdentity deviceIdentity = await credentialIdentityProvider.GetAsync(deviceId, userName, token, null); Assert.NotNull(deviceIdentity); IClientCredentials clientCredentials = (deviceIdentity as ProtocolGatewayIdentity)?.ClientCredentials; return(clientCredentials); }