/// <summary>
 /// Gets a list of all the roles for the configured applicationName.
 /// </summary>
 /// <returns>
 /// A string array containing the names of all the roles stored in the data source for the configured applicationName.
 /// </returns>
 public override string[] GetAllRoles()
 {
     if (!this.useWCFCacheService)
     {
         return((from t in this.storageCache.GetAuthorizedItems(this.storeName, this.applicationName, WindowsIdentity.GetCurrent().GetUserBinarySSid(), new string[0], DateTime.Now)
                 where t.Type == ItemType.Role
                 select t.Name).ToArray());
     }
     else
     {
         using (NetSqlAzManWCFCacheService.CacheServiceClient csc = new NetSqlAzManWCFCacheService.CacheServiceClient())
         {
             try
             {
                 return((from t in csc.GetAuthorizedItemsForWindowsUsers(this.storeName, this.applicationName, WindowsIdentity.GetCurrent().GetUserBinarySSid(), WindowsIdentity.GetCurrent().GetGroupsBinarySSid(), DateTime.Now, null)
                         where t.Type == NetSqlAzManWCFCacheService.ItemType.Role
                         select t.Name).ToArray());
             }
             finally
             {
                 ((IDisposable)csc).Dispose();
             }
         }
     }
 }
 /// <summary>
 /// Invalidates the cache.
 /// </summary>
 /// <param name="waitForCacheBuiltCompletition">if set to <c>true</c> [wait for cache built completition].</param>
 public void InvalidateCache(bool waitForCacheBuiltCompletition)
 {
     if (!waitForCacheBuiltCompletition && NetSqlAzManRoleProvider.buildingCache)
     {
         //Ignore build cache requests while building
         return;
     }
     else
     {
         lock (NetSqlAzManRoleProvider.locker)
         {
             try
             {
                 NetSqlAzManRoleProvider.buildingCache = true;
                 if (!this.useWCFCacheService)
                 {
                     this.storageCache.BuildStorageCache(this.storeName, this.applicationName);
                 }
                 else
                 {
                     using (NetSqlAzManWCFCacheService.CacheServiceClient csc = new NetSqlAzManWCFCacheService.CacheServiceClient())
                     {
                         try
                         {
                             csc.InvalidateCache();
                         }
                         finally
                         {
                             ((IDisposable)csc).Dispose();
                         }
                     }
                 }
             }
             finally
             {
                 NetSqlAzManRoleProvider.buildingCache = false;
             }
         }
     }
 }
        /// <summary>
        /// Gets a value indicating whether the specified user is in the specified role for the configured applicationName.
        /// </summary>
        /// <param name="username">The user name to search for.</param>
        /// <param name="roleName">The role to search in.</param>
        /// <returns>
        /// true if the specified user is in the specified role for the configured applicationName; otherwise, false.
        /// </returns>
        public override bool IsUserInRole(string username, string roleName)
        {
            if (this.userLookupType == "LDAP")
            {
                WindowsIdentity wid = null;
                if (String.Compare(((System.Threading.Thread.CurrentPrincipal.Identity as WindowsIdentity) ?? WindowsIdentity.GetCurrent()).Name, this.getFQUN(username), true) == 0)
                {
                    wid = ((System.Threading.Thread.CurrentPrincipal.Identity as WindowsIdentity) ?? WindowsIdentity.GetCurrent());
                }
                if (wid == null)
                {
                    wid = new WindowsIdentity(this.getUpn(this.getFQUN(username))); //Kerberos Protocol Transition: Works in W2K3 native domain only
                }

                if (!this.useWCFCacheService)
                    return (from t in this.storageCache.GetAuthorizedItems(this.storeName, this.applicationName, wid.GetUserBinarySSid(), wid.GetGroupsBinarySSid(), DateTime.Now)
                            where
                            t.Type == ItemType.Role
                            &&
                            (t.Authorization == AuthorizationType.Allow || t.Authorization == AuthorizationType.AllowWithDelegation)
                            &&
                            t.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase)
                            select t).Count() > 0;
                else
                {
                    using (NetSqlAzManWCFCacheService.CacheServiceClient csc = new NetSqlAzManWCFCacheService.CacheServiceClient())
                    {
                        try
                        {
                            return (from t in csc.GetAuthorizedItemsForWindowsUsers(this.storeName, this.applicationName, wid.GetUserBinarySSid(), wid.GetGroupsBinarySSid(), DateTime.Now, null)
                                    where
                                    t.Type == NetSqlAzManWCFCacheService.ItemType.Role
                                    &&
                                    (t.Authorization == NetSqlAzManWCFCacheService.AuthorizationType.Allow || t.Authorization == NetSqlAzManWCFCacheService.AuthorizationType.AllowWithDelegation)
                                    &&
                                    t.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase)
                                    select t).Count() > 0;
                        }
                        finally
                        {
                            ((IDisposable)csc).Dispose();
                        }
                    }
                }
            }
            else
            {
                using (IAzManStorage storage = new SqlAzManStorage(this.storageCache.ConnectionString))
                {
                    IAzManApplication application = storage[this.storeName][this.applicationName];
                    IAzManDBUser dbUser = application.GetDBUser(username);

                    if (!this.useWCFCacheService)
                        return (from t in this.storageCache.GetAuthorizedItems(this.storeName, this.applicationName, dbUser.CustomSid.StringValue, DateTime.Now)
                                where
                                t.Type == ItemType.Role
                                &&
                                (t.Authorization == AuthorizationType.Allow || t.Authorization == AuthorizationType.AllowWithDelegation)
                                &&
                                t.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase)
                                select t).Count() > 0;
                    else
                    {
                        using (NetSqlAzManWCFCacheService.CacheServiceClient csc = new NetSqlAzManWCFCacheService.CacheServiceClient())
                        {
                            try
                            {
                                return (from t in csc.GetAuthorizedItemsForDatabaseUsers(this.storeName, this.applicationName, dbUser.CustomSid.StringValue, DateTime.Now, null)
                                        where
                                        t.Type == NetSqlAzManWCFCacheService.ItemType.Role
                                        &&
                                        (t.Authorization == NetSqlAzManWCFCacheService.AuthorizationType.Allow || t.Authorization == NetSqlAzManWCFCacheService.AuthorizationType.AllowWithDelegation)
                                        &&
                                        t.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase)
                                        select t).Count() > 0;
                            }
                            finally
                            {
                                ((IDisposable)csc).Dispose();
                            }
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Invalidates the cache.
 /// </summary>
 /// <param name="waitForCacheBuiltCompletition">if set to <c>true</c> [wait for cache built completition].</param>
 public void InvalidateCache(bool waitForCacheBuiltCompletition)
 {
     if (!waitForCacheBuiltCompletition && NetSqlAzManRoleProvider.buildingCache)
     {
         //Ignore build cache requests while building
         return;
     }
     else
     {
         lock (NetSqlAzManRoleProvider.locker)
         {
             try
             {
                 NetSqlAzManRoleProvider.buildingCache = true;
                 if (!this.useWCFCacheService)
                 {
                     this.storageCache.BuildStorageCache(this.storeName, this.applicationName);
                 }
                 else
                 {
                     using (NetSqlAzManWCFCacheService.CacheServiceClient csc = new NetSqlAzManWCFCacheService.CacheServiceClient())
                     {
                         try
                         {
                             csc.InvalidateCache();
                         }
                         finally
                         {
                             ((IDisposable)csc).Dispose();
                         }
                     }
                 }
             }
             finally
             {
                 NetSqlAzManRoleProvider.buildingCache = false;
             }
         }
     }
 }
 /// <summary>
 /// Gets a list of all the roles for the configured applicationName.
 /// </summary>
 /// <returns>
 /// A string array containing the names of all the roles stored in the data source for the configured applicationName.
 /// </returns>
 public override string[] GetAllRoles()
 {
     if (!this.useWCFCacheService)
         return (from t in this.storageCache.GetAuthorizedItems(this.storeName, this.applicationName, WindowsIdentity.GetCurrent().GetUserBinarySSid(), new string[0], DateTime.Now)
                 where t.Type == ItemType.Role
                 select t.Name).ToArray();
     else
     {
         using (NetSqlAzManWCFCacheService.CacheServiceClient csc = new NetSqlAzManWCFCacheService.CacheServiceClient())
         {
             try
             {
                 return (from t in csc.GetAuthorizedItemsForWindowsUsers(this.storeName, this.applicationName, WindowsIdentity.GetCurrent().GetUserBinarySSid(), WindowsIdentity.GetCurrent().GetGroupsBinarySSid(), DateTime.Now, null)
                         where t.Type == NetSqlAzManWCFCacheService.ItemType.Role
                         select t.Name).ToArray();
             }
             finally
             {
                 ((IDisposable)csc).Dispose();
             }
         }
     }
 }
        /// <summary>
        /// Gets a value indicating whether the specified user is in the specified role for the configured applicationName.
        /// </summary>
        /// <param name="username">The user name to search for.</param>
        /// <param name="roleName">The role to search in.</param>
        /// <returns>
        /// true if the specified user is in the specified role for the configured applicationName; otherwise, false.
        /// </returns>
        public override bool IsUserInRole(string username, string roleName)
        {
            if (this.userLookupType == "LDAP")
            {
                WindowsIdentity wid = null;
                if (String.Compare(((System.Threading.Thread.CurrentPrincipal.Identity as WindowsIdentity) ?? WindowsIdentity.GetCurrent()).Name, this.getFQUN(username), true) == 0)
                {
                    wid = ((System.Threading.Thread.CurrentPrincipal.Identity as WindowsIdentity) ?? WindowsIdentity.GetCurrent());
                }
                if (wid == null)
                {
                    wid = new WindowsIdentity(this.getUpn(this.getFQUN(username))); //Kerberos Protocol Transition: Works in W2K3 native domain only
                }

                if (!this.useWCFCacheService)
                {
                    return((from t in this.storageCache.GetAuthorizedItems(this.storeName, this.applicationName, wid.GetUserBinarySSid(), wid.GetGroupsBinarySSid(), DateTime.Now)
                            where
                            t.Type == ItemType.Role
                            &&
                            (t.Authorization == AuthorizationType.Allow || t.Authorization == AuthorizationType.AllowWithDelegation)
                            &&
                            t.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase)
                            select t).Count() > 0);
                }
                else
                {
                    using (NetSqlAzManWCFCacheService.CacheServiceClient csc = new NetSqlAzManWCFCacheService.CacheServiceClient())
                    {
                        try
                        {
                            return((from t in csc.GetAuthorizedItemsForWindowsUsers(this.storeName, this.applicationName, wid.GetUserBinarySSid(), wid.GetGroupsBinarySSid(), DateTime.Now, null)
                                    where
                                    t.Type == NetSqlAzManWCFCacheService.ItemType.Role
                                    &&
                                    (t.Authorization == NetSqlAzManWCFCacheService.AuthorizationType.Allow || t.Authorization == NetSqlAzManWCFCacheService.AuthorizationType.AllowWithDelegation)
                                    &&
                                    t.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase)
                                    select t).Count() > 0);
                        }
                        finally
                        {
                            ((IDisposable)csc).Dispose();
                        }
                    }
                }
            }
            else
            {
                using (IAzManStorage storage = new SqlAzManStorage(this.storageCache.ConnectionString))
                {
                    IAzManApplication application = storage[this.storeName][this.applicationName];
                    IAzManDBUser      dbUser      = application.GetDBUser(username);

                    if (!this.useWCFCacheService)
                    {
                        return((from t in this.storageCache.GetAuthorizedItems(this.storeName, this.applicationName, dbUser.CustomSid.StringValue, DateTime.Now)
                                where
                                t.Type == ItemType.Role
                                &&
                                (t.Authorization == AuthorizationType.Allow || t.Authorization == AuthorizationType.AllowWithDelegation)
                                &&
                                t.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase)
                                select t).Count() > 0);
                    }
                    else
                    {
                        using (NetSqlAzManWCFCacheService.CacheServiceClient csc = new NetSqlAzManWCFCacheService.CacheServiceClient())
                        {
                            try
                            {
                                return((from t in csc.GetAuthorizedItemsForDatabaseUsers(this.storeName, this.applicationName, dbUser.CustomSid.StringValue, DateTime.Now, null)
                                        where
                                        t.Type == NetSqlAzManWCFCacheService.ItemType.Role
                                        &&
                                        (t.Authorization == NetSqlAzManWCFCacheService.AuthorizationType.Allow || t.Authorization == NetSqlAzManWCFCacheService.AuthorizationType.AllowWithDelegation)
                                        &&
                                        t.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase)
                                        select t).Count() > 0);
                            }
                            finally
                            {
                                ((IDisposable)csc).Dispose();
                            }
                        }
                    }
                }
            }
        }