Sub topic 9
Restricting logins by IP range and login hours
This chapter assumes you can navigate Setup and know how profiles and permission sets differ. It also assumes you have read the earlier chapters on session settings and MFA enablement. It covers the controls that limit where and when users can sign in, and how they work alongside MFA.
Three controls that look alike
Salesforce has three IP-related settings that are easy to confuse. Only one of them blocks logins by itself.
| Control | Where you set it | Scope | Effect on a login from outside the range |
|---|---|---|---|
| Login IP ranges | Profile | Users with that profile | Blocked |
| Trusted IP ranges | Setup, Network Access | Whole org | Allowed, but the user must verify identity |
| Login hours | Profile | Users with that profile | Blocked outside the allowed hours |
Login IP ranges and login hours live on the profile. Permission sets do not carry either setting. If you want to restrict a group of users, they need a shared profile, or you need to assign them to a dedicated one.
Login IP ranges
A login IP range is a start address and an end address. When a profile has at least one range, users with that profile can sign in only from addresses inside a range. A user outside the ranges is refused, even with a correct password and a valid MFA response.
To add a range:
- In Setup, open Profiles and select the profile.
- Find Login IP Ranges and click Add IP Ranges.
- Enter the start and end IP addresses and, optionally, a description.
- Save.
Salesforce asks for start and end addresses, not CIDR notation. If your network team gives you 203.0.113.0/24, convert it first. Python's standard library does this:
import ipaddress
cidr_blocks = ["203.0.113.0/24", "198.51.100.64/26"]
for block in cidr_blocks:
network = ipaddress.ip_network(block)
print(f"{block}: start={network[0]} end={network[-1]}")The output is 203.0.113.0 to 203.0.113.255 for the first block and 198.51.100.64 to 198.51.100.127 for the second.
Enforcement at login versus on every request
By default, Salesforce checks a profile's login IP ranges when the user logs in. That leaves a gap. A session started inside the office can keep working after the user moves to another network, for example if someone carries the laptop out of the building.
In Session Settings, the option Enforce login IP ranges on every request closes that gap. Salesforce then checks the ranges on each request, and a session that leaves the range stops working. Enable this option for profiles where the restriction is a real control and not a convenience.
A related session setting, Lock sessions to the IP address from which they originated, ties a session to the address that created it. The two settings answer different questions:
- Enforce login IP ranges asks whether the address is allowed by the profile.
- Lock sessions asks whether the address is the same as at login.
Which logins are covered
Profile IP ranges apply to API logins as well as browser logins. This matters for integrations. An integration user whose profile has ranges can connect only from those addresses. Add the outbound addresses of your middleware or integration platform to that profile, or the integration fails at login.
Connected apps have their own IP policy. In the connected app's OAuth policies, IP Relaxation controls whether the profile's ranges are enforced for that app. Relaxing the restriction for an app removes protection for that app's tokens, so use it only when you have another control in place.
Trusted IP ranges
Trusted IP ranges are set under Setup, Network Access. They do not block anyone. They mark addresses where Salesforce can skip the identity verification step, such as the emailed code that normally appears when a user logs in from an unrecognized browser or location.
For MFA, remember two points:
- Trusted IP ranges do not exempt a user from MFA. If MFA is required for the user, it is required from every address.
- For API logins, a login from a trusted IP range does not need the user's security token appended to the password. Logins from other addresses do.
The effect is the opposite of what the name suggests to many admins. Adding your office network here makes logins easier from the office. It does not make logins from elsewhere harder.
Login hours
Login hours restrict the days and times a profile's users can sign in. In Setup, open the profile, find Login Hours, click Edit, and set a start and end time for each day of the week. The times are interpreted in the org's default time zone, not each user's own time zone. A user in another region sees their allowed window shifted accordingly.
When the allowed window ends, users who are already signed in are not necessarily cut off immediately. Salesforce blocks their next request once the window has closed, and they are logged out. Warn users who work near the end of the window so they do not lose unsaved work.
Login hours suit profiles for shift-based or contractor users. They are a poor fit for integration users that run around the clock, and for admins who respond to incidents at any hour.
How the checks combine
A login passes through several gates. Any one of them can stop it.
The combination is useful. A stolen password and a stolen MFA response are still of no use to an attacker who is on the wrong network or logging in at the wrong time.
Avoiding a lockout
The most common error with these controls is locking out the person who set them. If you add a range that excludes your own address to your own profile, you cannot log in again.
Follow these practices:
- Test on a copy of the profile or on a single test user before you change a widely used profile.
- Confirm your own public IP address before you save. Your office, VPN, and home addresses differ.
- Remember that VPN users appear to come from the VPN's egress address, not their local one. Use that address in the range.
- Mobile users and remote staff change addresses often. Ranges suit fixed networks. For people who move around, rely on MFA and session settings instead.
- Keep at least one trusted admin with a documented recovery path. Salesforce Support can help in some cases, but do not plan around it.
Checking what was blocked
Blocked attempts show up in login history. This query lists unsuccessful logins from the past week with their source addresses:
SELECT UserId, LoginTime, SourceIp, Status, LoginType, Application
FROM LoginHistory
WHERE LoginTime = LAST_N_DAYS:7
AND Status != 'Success'
ORDER BY LoginTime DESC
LIMIT 200The Status field holds a text reason for each failure. Look at the distinct values in your own org rather than assuming their exact wording. A cluster of failures from one SourceIp for one user usually means that user is outside a range. The user might be on a VPN, for example. A spread of failures across many users from one address suggests someone is guessing passwords against your org.
After you change a range, run the query again to confirm the legitimate users stopped failing and the unexpected addresses are still refused.
Common problems
- A user is refused with the correct password. Compare their
SourceIpin login history to the profile's ranges. Check their VPN status. - An integration stopped working after a profile change. The integration's server address is probably outside the new range. Add it, or move the integration user to a profile with the right ranges.
- A user is logged out at an odd time. Check login hours and the org time zone.
- Ranges seem ignored after login. The session was created in range, and Enforce login IP ranges on every request is off.
- Trusted IP ranges did not stop an attacker. They are not meant to. Use login IP ranges for blocking.
Exercise
Your sales team works from a branch office with a fixed address range. Two reps often work from home. You want to block logins from outside the office, but the home users complain they can no longer sign in. What are two reasonable ways to handle this, and which setting would you enable so that a session started in the office stops working if the laptop leaves the office?
Show answer
Put the two home users on a separate profile without login IP ranges, and protect them with MFA. Alternatively, give them a VPN and add the VPN's egress address range to the profile. To end sessions that leave the office, enable "Enforce login IP ranges on every request" in Session Settings.
Exercise
An admin adds the office network to Trusted IP ranges and expects logins from other networks to be refused. Why does this not happen, and what should the admin do instead?
Show answer
Trusted IP ranges only mark addresses where Salesforce can skip identity verification. They do not block anything, and they do not exempt users from MFA. To refuse logins from other networks, add login IP ranges to the users' profile.
Exercise
A contractor profile has login hours of 08:00 to 18:00, but a contractor in another time zone is logged out in the middle of her afternoon. What is the likely cause?
Show answer
Login hours are interpreted in the org's default time zone, not the user's time zone. Her local afternoon falls after 18:00 in the org's time zone, so her next request is blocked and she is logged out.