As a tenant, you have rights when your landlord gets foreclosed, but you need to take action to protect those rights. The new owner may try to evict you, shut off your utilities, keep your security deposit or last month’s rent, or say you do not have legal rights after the foreclosure.
Start to protect your rights as soon as you learn your landlord might lose the property to foreclosure.
If you are a former owner of a foreclosed property, your legal rights are not the same as tenants. See: Rights of Former Homeowners After Foreclosure.