Entity authentication protocol by encryption
In general, no, this is not safe, and in the cases where it is safe it's wasteful.

The safe (but wasteful) cases are those where you're already using an Authenticated Encryption with Associated Data (AEAD) scheme or applying a Message Authentication Code (MAC) over the ciphertext. It's wasteful since those both have their own authentication systems, but safe since it prevents a MITM from modifying the ciphertext in transit.

Without authentication the MITM can simply modify data in the ciphertext beyond the start of $N$, ie anything in $ID_{B}$. This won't affect $N$ at all in any of the commonly used modes (either a direct stream cipher or a block cipher in CTR or CBC mode). There are some caveats to my statement, eg if $N$ is not a multiple of the underlying block size in CBC or CTR mode then data out to the next multiple of the block size needs to be preserved by the attacker, but since you didn't specify anything about the cipher used for encryption I'll go with the most general case.