public static bool TryParse(string value, out IotHubDeviceIdentity identity)
 {
     string[] usernameSegments = value.Split('/');
     if (usernameSegments.Length < 2)
     {
         identity = null;
         return false;
     }
     identity = new IotHubDeviceIdentity(usernameSegments[0], usernameSegments[1]);
     return true;
 }
 internal static IAuthenticationMethod DeriveAuthenticationMethod(IAuthenticationMethod currentAuthenticationMethod, IotHubDeviceIdentity deviceIdentity)
 {
     switch (deviceIdentity.Scope)
     {
         case AuthenticationScope.None:
             var policyKeyAuth = currentAuthenticationMethod as DeviceAuthenticationWithSharedAccessPolicyKey;
             if (policyKeyAuth != null)
             {
                 return new DeviceAuthenticationWithSharedAccessPolicyKey(deviceIdentity.Id, policyKeyAuth.PolicyName, policyKeyAuth.Key);
             }
             var deviceKeyAuth = currentAuthenticationMethod as DeviceAuthenticationWithRegistrySymmetricKey;
             if (deviceKeyAuth != null)
             {
                 return new DeviceAuthenticationWithRegistrySymmetricKey(deviceIdentity.Id, deviceKeyAuth.DeviceId);
             }
             var deviceTokenAuth = currentAuthenticationMethod as DeviceAuthenticationWithToken;
             if (deviceTokenAuth != null)
             {
                 return new DeviceAuthenticationWithToken(deviceIdentity.Id, deviceTokenAuth.Token);
             }
             throw new InvalidOperationException("");
         case AuthenticationScope.SasToken:
             return new DeviceAuthenticationWithToken(deviceIdentity.Id, deviceIdentity.Secret);
         case AuthenticationScope.DeviceKey:
             return new DeviceAuthenticationWithRegistrySymmetricKey(deviceIdentity.Id, deviceIdentity.Secret);
         case AuthenticationScope.HubKey:
             return new DeviceAuthenticationWithSharedAccessPolicyKey(deviceIdentity.Id, deviceIdentity.PolicyName, deviceIdentity.Secret);
         default:
             throw new InvalidOperationException("Unexpected AuthenticationScope value: " + deviceIdentity.Scope);
     }
 }
        internal static IAuthenticationMethod DeriveAuthenticationMethod(IAuthenticationMethod currentAuthenticationMethod, AzureIoTHUb.IotHubDeviceIdentity deviceIdentity)
        {
            switch (deviceIdentity.Scope)
            {
            case AzureIoTHUb.AuthenticationScope.None:
                var policyKeyAuth = currentAuthenticationMethod as DeviceAuthenticationWithSharedAccessPolicyKey;
                if (policyKeyAuth != null)
                {
                    return(new DeviceAuthenticationWithSharedAccessPolicyKey(deviceIdentity.Id, policyKeyAuth.PolicyName, policyKeyAuth.Key));
                }
                var deviceKeyAuth = currentAuthenticationMethod as DeviceAuthenticationWithRegistrySymmetricKey;
                if (deviceKeyAuth != null)
                {
                    return(new DeviceAuthenticationWithRegistrySymmetricKey(deviceIdentity.Id, deviceKeyAuth.DeviceId));
                }
                var deviceTokenAuth = currentAuthenticationMethod as DeviceAuthenticationWithToken;
                if (deviceTokenAuth != null)
                {
                    return(new DeviceAuthenticationWithToken(deviceIdentity.Id, deviceTokenAuth.Token));
                }
                throw new InvalidOperationException("");

            case AzureIoTHUb.AuthenticationScope.SasToken:
                return(new DeviceAuthenticationWithToken(deviceIdentity.Id, deviceIdentity.Secret));

            case AzureIoTHUb.AuthenticationScope.DeviceKey:
                return(new DeviceAuthenticationWithRegistrySymmetricKey(deviceIdentity.Id, deviceIdentity.Secret));

            case AzureIoTHUb.AuthenticationScope.HubKey:
                return(new DeviceAuthenticationWithSharedAccessPolicyKey(deviceIdentity.Id, deviceIdentity.PolicyName, deviceIdentity.Secret));

            default:
                throw new InvalidOperationException("Unexpected AuthenticationScope value: " + deviceIdentity.Scope);
            }
        }