Skip to content

Time providers

Rob Janssen edited this page Apr 29, 2019 · 2 revisions

Another set of providers in this library are the Time Providers; this library provides three 'built-in' ones. The default Time Provider used is the LocalMachineTimeProvider; this provider simply returns the output of DateTime.UtcNow and is highly recommended as default provider. The HttpTimeProvider executes a HEAD request against a given webserver (default: google.com) and tries to extract the Date:-HTTP header and returns it's date. Other url's/domains can be used by specifying the url in the constructor. You can easily implement your own TimeProvider by simply implementing the ITimeProvider interface.

As to why these Time Providers are implemented: it allows the TwoFactorAuth library to ensure the hosts time is correct (or rather: within a margin). You can use the EnsureCorrectTime() method to ensure the hosts time is correct. By default this method will compare the hosts time (returned by calling GetTimeAsync() on the LocalMachineTimeProvider) to the default HttpTimeProvider and NTPTimeProvider. You can pass an array of ITimeProviders and specify the leniency (second argument) allowed (default: 5 seconds). The method will throw when the TwoFactorAuth's timeprovider (which can be any ITimeProvider, see constructor) differs more than the given amount of seconds from any of the given ITimeProviders. We advise to call this method sparingly when relying on 3rd parties (which both the HttpTimeProvider and NTPTimeProvider do) or, if you need to ensure time is correct on a (very) regular basis to implement an ITimeProvider that is more efficient than the 'built-in' ones (like use a GPS signal). The EnsureCorrectTime() method is mostly to be used to make sure the server is configured correctly.

Clone this wiki locally