It is best to use the Abstract Factory pattern in conjunction with the Command Pattern. Which can reduce hard coding, as well as have loosely coupled code, so you can expand the functionality in the future for each authentication provider. Find a snippet of each section of code below
Abstract class for BaseAuthentication provider
public abstract class BaseAuthenticationProvider {
Use the following code snippet to implement a specific class for Gooogle, Yahoo, Microsoft, etc.
public class GoogleAuthentication : BaseAuthenticationProvider { public GoogleAuthentication() {
Factory class to create a specific object , to prevent the creation of an instance of this Factory class to implement a private constructor.
public class AuthenticationProviderFactory { private AuthenticationProviderFactory() { } public static BaseAuthenticationProvider GetInstance(string Domain) { switch (Domain) { case "google": return new GoogleAuthentication(); case "yahoo": return new YahooAuthentication(); } } }
Login.aspx: have buttons for each authentication provider, set a value for "CommandName" for each of the buttons and associate all buttons with the same event handler
eg. btn_google.CommandName = "google"
Protected Sub AuthenticationProvider_Click(sender As Object, e As EventArgs) Handles btn_google.Click, btn_yahoo.Click AuthenticationProviderFactory.GetInstance(((Button)sender).CommandName).AuthorizeUser(); End Sub
The appropriate AuthorizeUser method will invoke the appropriate provider site for authentication. When the provider redirects the user to the return URL, apply the same pattern in the Page_Load event and call the Autheticate method from the abstract class.