Exemple #1
0
        // Static Methods

        /// <summary>
        /// Creates a new <see cref="ISecurityProvider"/> based on the settings in the config file.
        /// </summary>
        /// <param name="username">Username of the user for whom the <see cref="ISecurityProvider"/> is to be created.</param>
        /// <returns>An object that implements <see cref="ISecurityProvider"/>.</returns>
        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);
        }