Пример #1
0
        public Task <bool> IsInRoleAsync(ApplicationUser user, string roleName, CancellationToken cToken)
        {
            string prefix = nameof(IsInRoleAsync) + Constants.FNSUFFIX;

            bool result = false;

            try
            {
                // Get the role to get the roleID
                ApplicationRole appRole;
                using (var rolesDAL = new AspNetRolesDAL(_connStr)) { appRole = rolesDAL.SelectByRoleName(roleName.ToUpper()); }

                if (appRole != null)
                {
                    // Get the roleIDs for the user
                    IEnumerable <string> roleIDsForUser;
                    using (var userRolesDAL = new AspNetUserRolesDAL(_connStr)) { roleIDsForUser = userRolesDAL.SelectRolesForUser(user.UserId); }

                    foreach (string roleID in roleIDsForUser)
                    {
                        if (string.Equals(roleID, appRole.RoleId, StringComparison.OrdinalIgnoreCase))
                        {
                            result = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(prefix + $"Exception:[{ex.ToString()}]");
            }

            return(Task.FromResult(result));
        }
Пример #2
0
        public Task <IList <string> > GetRolesAsync(ApplicationUser user, CancellationToken cToken)
        {
            string prefix = nameof(GetRolesAsync) + Constants.FNSUFFIX;

            IList <string> roles = new List <string>();

            try
            {
                IEnumerable <string> roleIDs = null;
                using (var userRolesDAL = new AspNetUserRolesDAL(_connStr)) { roleIDs = userRolesDAL.SelectRolesForUser(user.UserId); }

                if (roleIDs.Any())
                {
                    using (var rolesDAL = new AspNetRolesDAL(_connStr)) { roles = rolesDAL.SelectRoleNamesByRoleId(roleIDs).ToList(); }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(prefix + $"Exception:[{ex.ToString()}]");
            }

            return(Task.FromResult(roles));
        }