public static ISecurityProvider CreateProvider(string username)
{
// Initialize the username.
if (string.IsNullOrEmpty(username))
username = Thread.CurrentPrincipal.Identity.Name;
// If an application is being launched from an installer it will have the NT AUTHORITY\System Identity which
// will not have available user information - so we pickup username from Environment instead
if (username.StartsWith("NT AUTHORITY\\", StringComparison.OrdinalIgnoreCase))
username = Environment.UserDomainName + "\\" + Environment.UserName;
// Instantiate the provider.
// ReSharper disable once AssignNullToNotNullAttribute
ISecurityProvider provider = Activator.CreateInstance(Type.GetType(s_providerType), username) as ISecurityProvider;
if ((object)provider == null)
throw new InvalidOperationException(string.Format("Failed to acquire security provider from '{0}'. Specified class does not implement ISecurityProvider.", s_providerType));
// Initialize the provider.
provider.Initialize();
// Return initialized provider.
return provider;
}