united-states
If sexual consent is gained by deceit or misapprehension, is the
resulting sex legally a rape?
The predominant rule in U.S. law is that sexual consent gained by deceit or misapprehension is not legally a rape. This general rule is one of the sources of the aphorism, "all's fair in love and war."
But this rule is almost always subject to an exception that applies in this fact pattern.
The exception to the general rule is that sexual consent secured by deceit or misapprehension about who you have having sex with (e.g. blindfolding someone and deceiving them to think that they are having sex with their spouse) is rape.
This rule (sometimes statutory and sometimes a common law rule) mirrors the statutory rule in New South Wales cited by @DaleM in his answer.
This exception applies when: "Merlin magically disguises Uther Pendragon to look like his enemy Gorlois, enabling Uther to sleep with Gorlois' wife Lady Igraine."
Most jurisdictions also treat sexual penetration or contact secured by pretending to provide medical care or a lawful law enforcement search to be rape. But, most often, the law structures this as an immunity for people who are doing so properly, rather than as consent obtained by fraud for those who do so when they don't qualify for this immunity from rape liability. This is because legitimate law enforcement searches and legitimate medical treatment is not always consensual and/or sometimes involves someone incapable of consent - like medical care provided to an unconscious person.
As the statutory language quoted by @Rick indicates, however, in England and Wales, impersonation of a law enforcement officer or medical practitioner, for example, with a lawful purpose, is structured, at least in part, as a lack of consent due to fraud issue.
If someone knows who they are having sex with, however, and are induced to have sex, for example, by a lie about how rich they are, or that they intend to pay a prostitute when that person does not intend to do so, or about not having an STD, or about planning to marry that person, these falsehoods do not cause the sexual conduct to be rape.
would the situation be different if there was no deception? One can
imagine a situation where two identical twins were separated at birth.
Twin A gets married, but whilst he is out of town Twin B happens to
meet his wife, and they end up having sex. The wife believed she was
sleeping with her husband; Twin B believed it was an innocent one
night stand. Since the wife's consent was contingent on a
misapprehension, was she unable to freely consent, and thus the act
was a rape?
For purposes of a criminal prosecution, it is the intent and knowledge of the criminal defendant, and not the intent and knowledge of the victim, that is relevant. Under the Model Penal Code (not adopted in any state but used as a model for particular criminal code definitions in many U.S. states) and under most U.S. state criminal codes, the minimum level of culpability for rape is recklessness, but this varies considerably from U.S. state to U.S. state. Some require, for example, knowledge, rather than mere recklessness as to whether or not they have secured consent without deceit that causes the act to be rape.
Whilst Igraine freely agrees to sex, she is under the misapprehension
that Uther is her husband; would that make her legally incapable of
giving informed consent?
The standard is consent, not informed consent. Informed consent is not required to avoid criminal liability for rape. Informed consent means that someone has been expressly told about enough of the material pros and cons of doing something to reasonably understand the risks and benefits of doing so, and then deciding to goes forward with something after having been informed in this manner. But this examination of the pros and cons of having sex which would be necessary to provide informed consent is not legally required for an action to not constitute rape (or for that matter, to consent to marriage).