GSF.Security.SecurityProviderUtility.CreateProvider C# (CSharp) Method

CreateProvider() public static method

Creates a new ISecurityProvider based on the settings in the config file.
public static CreateProvider ( string username ) : ISecurityProvider
username string Username of the user for whom the is to be created.
return ISecurityProvider
        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;
        }

Usage Example

Esempio n. 1
0
        // Static Methods

        /// <summary>
        /// Creates a new provider from data cached by the <see cref="SecurityProviderCache"/>.
        /// </summary>
        /// <param name="username">The username of the user for which to create a new provider.</param>
        /// <returns>A new provider initialized from cached data.</returns>
        public static ISecurityProvider CreateProvider(string username)
        {
            CacheContext cacheContext;

            lock (s_cache)
            {
                cacheContext = s_cache.GetOrAdd(username, name => new CacheContext(SecurityProviderUtility.CreateProvider(username)));
            }

            ISecurityProvider provider = SecurityProviderUtility.CreateProvider(cacheContext.Provider.UserData);

            AutoRefresh(provider);

            return(provider);
        }
All Usage Examples Of GSF.Security.SecurityProviderUtility::CreateProvider