Those of you familiar with Perforce will know that Domain Authentication is achieved by means of a server side trigger which calls an executable to perform an ActiveDirectory validation of the user and password passed. Perforce has a tool for this which they provide free of charge, documented in their Knowledge Base (article 74). However the code is hard to read (predominantly because of the ActiveDirectory C++ API), and the command line is far more complicated than it need be.
The example below is an attempt at making this tool significantly easier to read and use,while using F# combined with log4net. There are 3 files to the solution:
The result is a tool that only requires a domain and user name to be passed from the Perforce trigger to authenticate the user, and to log Perforce login activity to any number of destinations.
Using an 'AssemblyInfo' file in F# is just like in C# except maybe for the closing '()' which is a do-binding; MSDN has an article with the details. The only thing that is really different and important to know in F# is that file order matters, this means that in our example 'AssemblyInfo.fs' must come before 'auth.fs' in the '.fsproj' file, more details below.
The F# code for the main ActiveDirectory validation looks and feels just like C#, even the inclusion of log4net isn't any different, it all just tastes like chicken. However there is an important difference, above I mentioned that file order matters in a F# project, and that's because of the ' ' attribute, it tells F# where to begin execution. The ' ' attribute must be part of the last '.fs' file in a F# project, otherwise the compiler will complain about its location.
The app.config file is no different than with any other .NET language and because its using log4net, user authentication from Perforce can now be logged in any number of ways, in this case I choose to write to the EventLog.