Multi-Factor Authentication with Password and Private Key File in SSH.NET

If you want to use SSH.NET to connect to an SFTP server that requires both a password and a private key file, you’ll be happy to know that it is well supported! Unfortunately (and similar to my previous SSH.NET post about modifying the Host Key Algorithm) the documentation doesn’t really make it clear how to do this.

As an example, let’s grab the code sample from the SSH.NET documentation on multi-factor authentication. At time of writing it looks as follows:

var connectionInfo = new ConnectionInfo("sftp.foo.com",
    "guest",
    new PasswordAuthenticationMethod("guest", "pwd"),
    new PrivateKeyAuthenticationMethod("rsa.key"));

using (var client = new SftpClient(connectionInfo))
{
    client.Connect();
}

If you modify the server address, username and password and then run it you’ll quickly hit a brick wall in the shape of this error message:

Renci.SshNet.Common.SshAuthenticationException: Permission denied (publickey).

The first issue that we’re running into is that we’re not actually specifying the private key details anywhere! Let’s change that (I’ve also swapped the literal strings out for some variables):

string _privateKeyPath = "";
string _privateKeyPassPhrase = "";
string _host = "";
string _username = "";
string _password = "";

var keyFile = new PrivateKeyFile(_privateKeyPath, _privateKeyPassPhrase);
var keyFiles = new[] { keyFile };

var connectionInfo = new ConnectionInfo(_host, _username,
    new PasswordAuthenticationMethod(_username, _password),
    new PrivateKeyAuthenticationMethod("rsa.key", keyFiles));

using (var client = new SftpClient(connectionInfo))
{
	client.Connect();
}

Now if you run this you’ll get a new error (#ProgressIsProgress), this time telling you that you’re not allowed to change username:

Renci.SshNet.Common.SshConnectionException: The connection was closed by the server: Change of username or service not allowed: (<USERNAME>,ssh-connection) -> (rsa.key,ssh-connection) (ProtocolError).

Now when I first ran into this error and threw it into Google I got very few results; looks like we’re treading new ground!

The fix for this is actually really simple; the constructor for PrivateKeyAuthenticationMethod takes a username as the first parameter! For some reason this is set to “rsa.key” in the official documentation rather than “guest” which they use as the username placeholder on the two lines above, so when I first looked into this issue I wrongfully assumed that it was a string specifying what type of private key was being consumed!

The only change we need to make to the previous example is to swap in the _username variable (or a literal string of the username if you prefer) for the first parameter of the PrivateKeyAuthenticationMethod constructor.

string _privateKeyPath = "";
string _privateKeyPassPhrase = "";
string _host = "";
string _username = "";
string _password = "";

var keyFile = new PrivateKeyFile(_privateKeyPath, _privateKeyPassPhrase);
var keyFiles = new[] { keyFile };

var connectionInfo = new ConnectionInfo(_host,
    _username,
    new PasswordAuthenticationMethod(_username, _password),
    new PrivateKeyAuthenticationMethod(_username, keyFiles));

using (var client = new SftpClient(connectionInfo))
{
	client.Connect();
}

Run it now, and you should connect successfully!

Regular Expression Tips for People That Hate Regular Expressions

A screenshot of a code editor. There are three lines, setting the variables "isCustomerNumber", "isProductNumber" and "isTransactionNumber" based on whether a series of regular expression (regex) matches succeed

I’ve been writing regular expressions (regex) for years and would consider myself pretty good at them by now (I decline to provide references). The problem is that although it’s common to see regex as cryptic and painfully terse (which are not invalid points), they remain one of the best ways to handle complex pattern matching and as such you’ll probably run into them at least a few times in your career.

Even if you really despise writing regular expressions, here are some tips that will help to make that process as painless as possible!

Continue reading “Regular Expression Tips for People That Hate Regular Expressions”

Epson ET-2711/ET-2710 Series Printer Wireless Setup Made Easy

A photo of an Epson ET-2711 printer

If you want to just get to the good stuff, scroll down to the “Setup” section.

My wife and I finally got a new printer to replace the old one that has pretty much given up the ghost and refuses to print anymore (often considered an important feature for devices like this).

We settled on an Epson ET-2711 in large part because of the ink tank that should (hopefully) do away with the constant stream of expensive cartridges in favour of some cheaper-per-page ink bottles. I was excited to get it out and start setting it up (in part because we’re in another lockdown and what else is there to do right now)!

Imagine my frustration, dear reader, when I found out that the whole wireless setup process for this printer is terrible if you follow the official instructions. I genuinely spent a couple of hours bouncing between the Android app (which gave me a very helpful message telling me “Communication error”, to check the printer is on and not much else), the included software for Windows (which was just as helpful), the printed documentation in the box and the website, all to no avail.

You cannot convince me that this is a helpful error message

The good news is that I finally figured out how to get it working. The even better news for you at home is that I’ve gone through the process three times so that I could document all of the steps to get this working properly!

Continue reading “Epson ET-2711/ET-2710 Series Printer Wireless Setup Made Easy”

Migrating from KeePass to 1Password

An image of the KeePass and 1Password logos, with an arrow pointing from KeePass to the stylised "o" in 1Password

I’ve been using KeePass for my password management needs since early 2014, but I recently decided that I wanted to move to something a bit more substantial. KeePass is an excellent (open source!) piece of software, but since it stores passwords in a single database file the only way that I could really sync it between multiple computers and my phone was to store that file… in Dropbox.

Yeah, as you can imagine that’s not a great solution.

Anyway, I wanted to move over to 1Password but when I tried searching for “Migrate KeePass to 1Password” I kept seeing references to 3rd party scripts I’d need to use to convert my exported data (no thanks) or that it would require a bunch of manual data entry. After giving it a go though, I can tell you it was actually ridiculously easy and straightforward and didn’t require any external tools or scripts! As such, I decided to put together this step-by-step tutorial for migrating your data from KeePass to 1Password!

Continue reading “Migrating from KeePass to 1Password”

So You Want to Run Azure Functions Using .NET 5

I’ve spent the last couple of evenings trying to get a set of Azure Functions migrated from .NET Core 3.1 to .NET 5 so that I can play around with some of the nice new syntax options, nullable contexts and the like. Since .NET 5 has officially been released, it would be justifiable to believe that it would be well supported across the core Microsoft product catalogue, such as in… say… Azure Functions?

Ha! You’re a funny one.

Continue reading “So You Want to Run Azure Functions Using .NET 5”

There Is No Such Thing as a New Idea, and That’s Ok!

A lightbulb laying on a chalkboard, with a thought bubble drawn around it in chalk

Mark Twain once wrote that “there is no such thing as a new idea” and quite frankly I’m starting to agree with him.

I’ve spent the last month or so actively trying to come up with some ideas for projects/ blog posts to work on that would be interesting and useful. The issue I’ve run into is that almost every time I come up with something, I give it a quick search only to find out that it’s already been done (sometimes many times over). That means another idea goes on the pile to be abandoned since “there’s no point in doing it anymore I guess”.

Well that’s not a fun feeling to have.

Changing Host Key Algorithm in SSH.NET

I’ve used SSH.NET a lot over the years to send and receive files using SFTP and it’s a very flexible and practical library, but the documentation can be a bit thin on the ground when you’re looking to use some of the more esoteric features it has.

As an example, I recently ran into an issue where I was connecting to a remote server and the host fingerprint I was receiving through SSH.NET didn’t match the one that I expected to see (and could see in WinSCP). After verifying that I was using the same connection settings on both and more than a little spelunking through the SSH.NET source code I found that by default the host key algorithms used by the stable release of SSH.NET that I was on (2016.1.0) are RSA and DSA, while WinSCP uses Ed25519. For my purposes I needed to use Ed25519 in SSH.NET as well even though the SFTP host also supported these other algorithms.

Continue reading “Changing Host Key Algorithm in SSH.NET”

SoundFloored: Open Source Soundboard Pedal (Part 4 – Final Implementation)

The top of the SoundFloored pedal. It is held together by various cross-head bolts and has six footswitches embedded in the top, with four along the close long edge and two along the far long edge (with a screen embedded in the lid between them)

This is the fourth and final post in a series about designing and creating SoundFloored, an open source soundboard pedal! Check out the other posts in the series:


So SoundFloored is now working just like I wanted it to with buttons and a screen, but the current build isn’t exactly what I would call robust. As such, the final part of the build involves moving what I’ve built so far into a more permanent home, one that can withstand the rigours of live performance and is far more practical to move around.

Building the Box

Design

Based on some preliminary research on the minimum usable distance between footswitches, I decided to keep them 7cm apart from each other to ensure good clearance. I also thought to leave a 3.5cm gap between the switches and the edge of the pedal since I didn’t need as large of a gap but still wanted the numbers to map out in a simple way.

Since there were going to be four footswitches across the length of the pedal, it would need to be 28cm long (3 x 7cm for the gaps between each switch, plus 2 x 3.5cm for the gaps between the outer switches and the edge). Similar maths for the two footswitches going across the width of the pedal gives us 14cm (7cm for the gap plus 2 x 3.5cm for the edges).

I wanted to find something with these dimensions (or near enough) that I could use to house everything, so I started by looking at “project boxes” (plastic boxes designed for maker projects like this). Unfortunately, the largest project box I could easily order was barely half the size that I needed and I wasn’t interested in putting in a custom order to create one. The only option left at this point was to build something myself!

I figured that wood would be the best material for this project; although not the traditional material for a pedal, I could make it pretty much whatever size I wanted, it’s readily available, cheap and I already have some basic experience in woodworking.

At this point even though I had the dimensions I wanted to get a better perspective of how big the pedal was going to be before I started building anything. Mediocre arts and crafts to the rescue!

Continue reading “SoundFloored: Open Source Soundboard Pedal (Part 4 – Final Implementation)”

SoundFloored: Open Source Soundboard Pedal (Part 3 – Breadboard Implementation)

A Raspberry Pi connected to a breadboard with various wires. The breadboard has a screen on it that says "Memes" on the first row and "RS: STOP" on the second

This is the third post in a series about designing and creating SoundFloored, an open source soundboard pedal! Check out the other posts in the series:


So this project has really been gaining some steam! I’ve figured out the design and managed to get a software implementation with two separate interfaces. Now though, I’m entering uncharted territory; the world of hardware electronics.

Now I say uncharted territory, but I’d be remiss if I didn’t mention that I’ve previously watched a Pluralsight course called something like “Introduction to Electronics” or “Electronics Fundamentals”, although it’s also worth mentioning that I remember approximately 5% of the content. Actually, one of the few things I did remember was how a breadboard works, which is a place to start at least!

Preparation

So although I’ve owned a number of Raspberry Pis over the years and have a lot of HATs/pHATs to go with them, I’ve never ventured far enough into the hardware electronics side of things to have bought any components. As such, I ordered a pretty generic looking “Electronics Fun Kit” that had pretty much everything I might need for this project; a breadboard, wires (especially wires that I could use to connect the Pi directly to the breadboard), buttons, LEDs, resistors etc. I’m sure there are higher quality components out there, but since this is all for prototyping I decided that in this case cheap and cheerful was what I was looking for.

An ELEGOO Electronics Fun Kit, which is comprised of a cardboard box and various electronics components such as wires, buttons, capacitors, resistors etc.
Continue reading “SoundFloored: Open Source Soundboard Pedal (Part 3 – Breadboard Implementation)”

SoundFloored: Open Source Soundboard Pedal (Part 2 – Software Implementation)

A diagram of the SoundFloored Architecture. There is one long red box at the bottom with the text "PyGame" inside. Above that is a single green box that says "SoundFloored Logic", with three seperate side-by-side blue boxes above it that say "Raspberry Pi", "Gui" and "Keyboard" respectively.

This is the second post in a series about designing and creating SoundFloored, an open source soundboard pedal! Check out the other posts in the series:


So now that I’ve prepared what I can, it’s now the step that can often derail the whole project: implementation.

For context the “Plan” section of this post was written before I started writing any code and “Implementation” was written shortly after I’d put the first version together.

Plan

I know, I know, I’ve already done the planning in the first part, “when are you actually going to start writing code“?

Well, except for tiny projects (usually single file, one-off scripts) I like to spend some time considering how the different parts will interact. The reasoning is that as soon as you start working with multiple modules and classes you’ve really got to consider how the parts will hang together, including what should “own” which parts of the logic, what the different interfaces should be and how the code should be laid out.

Continue reading “SoundFloored: Open Source Soundboard Pedal (Part 2 – Software Implementation)”