They could. Consulates are not foreign soil. There are, however, conventions and protocol to follow and cops don’t bust up embassies.
To elaborate, we do not have extraterritoriality. While it may be illegal and punishable by death to be gay in Iran, if someone without diplomatic immunity murdered a gay man at the Iranian embassy in NYC, that person could and would still be charged with a crime as the crime happened in the US, not Iran. That we wouldn’t violate the embassy’s security by barging in with cops is a separate and distinct matter.