How to protect a lock against rusting
It sounds like you're not talking about the actual key lock itself or the pins &amp; tumblers, and I think you only mean the steel D bar &amp; latch, very similar to car door hinges &amp; latches, so a similar solution should work.

The owner's manuals for cars I've had recommended using one of these two, re-applied every few months or whenever it seems harder to open:


Grease -  white lithium grease (often used on car door hinges &amp; latches), or even any grease, wheel bearing/axle grease, even cooking grease for a short while, just about any type that won't wash away easily.
Oil - plain motor oil, or a 3-in-1 type


You could probably still use regular WD-40, it is very good at preventing rust, but it does go dry &amp; a little sticky pretty quickly, so you'd have to re-apply it much more often, maybe every month or so.

Or, you could use a specialty corrosion inhibitor spray like:


WD-40 Specialist Long Term Corrosion Inhibitor Spray - The company website advertises a comparison against some other methods with this image 
but Amazon might have some less biased reviews.


Or a firearms product like Birchwood Casey RIG Universal gun Grease that has reviews like "I have used this product since 1951... and never had any problems with rust."

I've also read that petroleum jelly or anything wax-like, even beeswax can be effective too. Probably anything that coats the metal and keeps water &amp; air away should work, it just depends on what you've got handy &amp; how often you want to re-apply it.