I did not interpret those scriptures out of context and I also properly harmonized scripture with scripture before reaching my conclusion on doctrine.
Matthew 7:21 - Not everyone who says to Me, 'Lord, Lord,' will enter the kingdom of heaven, but he who does the will of My Father who is in heaven will enter.
John 6:40 - For my Father’s will is that everyone who looks to the Son and believes in Him shall have eternal life, and I will raise them up at the last day.
These many people (unbelievers) in Matthew 7:22 had the wrong foundation. They were trusting in their works to save them and NOT IN CHRIST ALONE. Jesus NEVER knew them which means they were NEVER saved. Their hearts were not right with God, so their "attempted external obedience" (apart from the righteousness of God which is by faith and the blood of Christ) was stained with sin. Seeking salvation by works is not the will of the Father.
John 17:3 - And this is eternal life, that they may know You, the only true God, and Jesus Christ whom You have sent. The term "know" implies intimate, experiential knowledge, through a relationship with Him, not merely theoretical knowledge. These many people were not true converts.
Matthew 7:22 - And then I will declare to them, ‘I never knew you; depart from Me, you who PRACTICE lawlessness’... in contrast with putting the words of Jesus into PRACTICE. (Matthew 7:24) As we read on from Matthew 7:24 through verse 27, we find two different foundations with two different results, and not salvation by works, as some would suppose. Each house has a different material upon which its foundation is laid, and each house has a different final outcome. One house is built by a wise man upon a rock and it stands. The other is built by a foolish man upon the sand and it collapses. The rock the wise man builds upon is true righteousness found in Jesus Christ alone. The sand the foolish man builds upon is self-righteousness. Only those who truly believe in Him (demonstrative evidence) are wise and hear the words of Jesus and properly act on them.
Refusing to obey God is the (demonstrative evidence) that we have not yet come to know God.
You must not confuse conditions for coming to know Him/keeping His teachings with actions that demonstrate we have already come to know Him. You also must not confuse "descriptive" passages of scripture with "prescriptive" passages of scripture. The end result is salvation by works.