No act of a corporation formed under this chapter, or of a corporation formed under any other statute or special act having as its purpose or among its purposes a purpose for which a corporation may be formed under this chapter, and no transfer of real or personal property to or by such a corporation, otherwise lawful, shall be invalid by reason of the fact that the corporation was without capacity or power to do such act or to make or receive such transfer, but such lack of capacity or power may be asserted:
1. In an action by a stockholder or member against the corporation to enjoin the doing of any act or the transfer of real or personal property by or to the corporation. If the unauthorized act or transfer sought to be enjoined is being, or is to be, performed or made under any contract to which the corporation is a party, the court may, if all of the parties to the contract are parties to the action and if it deems the same to be equitable, set aside and enjoin the performance of such contract, and in so doing may allow to the corporation or to the other parties to the contract, as the case may be, such compensation as may be equitable for the loss or damage sustained by any of them from the action of the court in setting aside and enjoining the performance of such contract; provided that anticipated profits to be derived from the performance of the contract shall not be awarded by the court as a loss or damage sustained.2. In an action by or in the right of the corporation to procure a judgment in its favor against an incumbent or former officer or director of the corporation for loss or damage due to his unauthorized act.3. In an action or special proceeding by the superintendent or the attorney-general to annul or dissolve the corporation or to enjoin it from the doing of unauthorized business.