- Building an Interface
- Using Attributes
- The Test UI Application
- Summary
Using Attributes
The other class that will be added to the AuthenticationPlugin.Common project and namespace is a class that extends the .NET Framework Attribute class, called AuthenticationPluginAttribute. This class allows metadata to be defined that identifies the class that implements the IAuthenticationPlugin interface and will be loaded by the application.
There is certainly more than one way to skin this cat, and using metadata is only one of them. One alternative is to use method info to iterate through the types in an assembly to find the type that has IsLoginValid as a method. If anything, the use of an attribute here is to demonstrate how one can be used in reflection.
Another technique used to load types might be to stick the type in a configuration file used by the application. The problem with it, for this plug-in system, is that it doesn't work well with how the application works. The test application prompts the user for a file, then loads it. The assumption, therefore, might be that the user doesn't know the type inside the assembly.
The attribute, AuthenticationPluginAttribute, is listed in Listing 1.2. It contains nothing fancy—it doesn't require any parameters, as it will only be used to identify a class. The AuthenticationPluginAttribute class inherits from the Attribute class provided with .NET:
public sealed class AuthenticationPluginAttribute : Attribute
The class is sealed because there is no reason to inherit from it. However, it inherits all of the properties and methods provided by the Attribute class.
To use the attribute in the first test plug-in, [AuthenticationPlugin] is typed directly above the class declaration of the plug-in. The plug-in must also include and reference the AuthenticationPlugin.Common namespace, but it had to do that to use the AuthenticationPlugin interface.
Listing 1.2
using System; namespace AuthenticationPlugin.Common { /// <summary> /// The AuthenticationPluginAttribute is used to identify /// the main class of an Authentication plugin. /// </summary> [AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=true)] public sealed class AuthenticationPluginAttribute : Attribute { } }
The MyTest Plug-in
The MyTestAuthenticator class is a working—albeit simple—example plug-in. It implements the IAuthenticationPlugin interface, is identified by [AthenticationPlugin] attribute, and is included in its own project, which compiles to an assembly file called AuthenticationPlugin.Plugins.MyTest.dll. This plug-in simply compares the username and passwords to hard-coded values.
If you are developing your own plug-ins, you'd replace this hard-coded comparison with something more complicated, such as comparing the username and password against database values, a flat file, an XML file, or even Active Directory. Some of these will be covered in the rest of the articles in this series.
Our first plug-in is shown in its entirety in Listing 1.3. Notice that aside from the System namespace, the only namespace that it needs to reference is the AuthenticationPlugin.Common namespace. It is very useful to keep the dependencies as light as possible for these plug-ins.
Listing 1.3
using System; using AuthenticationPlugin.Common; namespace AuthenticationPlugin.Plugins.MyTest { /// <summary> /// A Test Authentication plugin /// </summary> [AuthenticationPlugin] public class MyTestAuthenticator : IAuthenticationPlugin { /// <summary> /// The method that is called to do the authentication and /// return a result. /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <returns></returns> public bool IsLoginValid( string username, string password ) { return username.Equals( "foo" ) && password.Equals( "bar" ); } } }
The steps to create another plug-in are:
- Add using AuthenticationPlugin.Common to the using block.
- Implement the IAuthenticationPlugin by tacking : IAuthenticationPlugin on to the end of the class declaration and by adding the IsLoginValid method.
- Identify the plug-in class by adding AuthenticationPlugin above the class declaration.