Sub topic 11
Reviewing login history
This chapter assumes you can navigate Setup, you know how profiles and permission sets grant access, and you have read the earlier chapters on MFA, session settings, and login IP restrictions. Here you will learn how to read Salesforce's login records, query them with SOQL, and turn them into a routine that catches problems early.
Why login history matters
Controls such as MFA, IP ranges, and login hours decide who can sign in. Login history tells you what actually happened. It answers questions like these:
- Is someone trying many passwords against one account?
- Are people signing in from countries where you have no staff?
- Which users are still signing in without MFA?
- Did a new IP restriction block people it should not have?
Review it on a schedule, and also after any change to login settings.
Where Salesforce records logins
Salesforce keeps login records in several places. They differ in what they hold and how long they keep it.
| Source | Where to find it | What it holds | Retention |
|---|---|---|---|
| Login History page | Setup, Login History | Up to 20,000 of the most recent login records, with CSV and GZIP download | Records for the last 6 months are available through download |
| `LoginHistory` object | SOQL, API, Developer Console | The same records, in queryable form | Last 6 months |
| `LoginEvent` (Event Monitoring) | Event Monitoring license or Shield | Richer fields, such as geography and MFA-related detail | Stored as big objects, longer retention |
| Setup Audit Trail | Setup, View Setup Audit Trail | Changes to settings, not logins | 180 days |
Check the current limits for your edition, because retention and availability change between releases. The rest of this chapter uses LoginHistory, which every org has.
Fields worth knowing
Open the Login History page and you will see these columns. The same data is exposed as fields on the LoginHistory object.
| Field | Meaning |
|---|---|
| `UserId` | The user who attempted the login |
| `LoginTime` | When the attempt happened, in UTC through the API |
| `LoginType` | How the user signed in, such as Application, Remote Access Client, or SAML SSO |
| `SourceIp` | The IP address the request came from |
| `Status` | `Success`, or a failure reason such as `Invalid Password` or `Failed: Computer activation required` |
| `Application` | The client used, such as a browser or a connected app |
| `Browser` and `Platform` | Client details taken from the request |
| `LoginUrl` | The host the user signed in at |
| `AuthenticationServiceId` | The SSO provider that handled the login, when one did |
LoginType is the field that connects this chapter to earlier ones. A user whose LoginType is Application signed in with a username and password on the standard login page. A SAML login came through your identity provider, so MFA, if any, happened there. An API client appears as a remote access type, which is why integration users deserve a separate look.
How a login becomes a record
Failures are as useful as successes. A failure tells you which control stopped the attempt, and a cluster of failures from one address is a pattern you can act on.
Reading the Login History page
Go to Setup and search for Login History. The page lists recent attempts for all users. From it you can:
- Scan the most recent records for failures.
- Select Download Login History to get a CSV or GZIP file for the last six months.
- Open the file in a spreadsheet and filter by
Status,SourceIp, orLoginType.
The page works for a quick check. For anything repeated, use SOQL, because a saved query gives the same result each time and can filter precisely.
Querying recent logins
The first query returns the 200 most recent logins from the past seven days, with the source address and result of each.
SELECT UserId, LoginTime, LoginType, SourceIp, Status, Application
FROM LoginHistory
WHERE LoginTime = LAST_N_DAYS:7
ORDER BY LoginTime DESC
LIMIT 200Run it in the Developer Console Query Editor, in Workbench, or through the REST API. Some points about this query:
LAST_N_DAYS:7is a date literal. It covers the past seven days relative to when you run the query.ORDER BY LoginTime DESCputs the newest records first, so aLIMITcuts off the oldest rows and not the newest.UserIdis an ID, not a name. To show names, query the related fieldUser.Name, for exampleSELECT User.Name, LoginTime, Status FROM LoginHistory.
Narrowing to failures
To focus on problems, exclude successes:
SELECT User.Username, LoginTime, SourceIp, Status, LoginType
FROM LoginHistory
WHERE LoginTime = LAST_N_DAYS:1
AND Status != 'Success'
ORDER BY LoginTime DESC
LIMIT 200Read the Status values as clues:
Invalid Passwordrepeated for one user suggests a forgotten password or a guessing attempt. Check whether theSourceIpvalues are all the same.- The same failure across many different users from one
SourceIpsuggests password spraying, where an attacker tries a common password against many accounts. - A failure about computer activation or verification means the identity check was triggered. This is the control working, but frequent cases for one user may mean the person registered no usable method.
- A failure from an address outside your login IP ranges confirms that the restriction is working.
Finding logins by address
To see who signed in from an unexpected address, filter on SourceIp:
SELECT User.Username, LoginTime, Status, LoginType
FROM LoginHistory
WHERE SourceIp = '203.0.113.45'
AND LoginTime = LAST_N_DAYS:30
ORDER BY LoginTime DESCThe address 203.0.113.45 is a documentation example. Replace it with an address from your own records.
Counting without reading every row
Aggregate queries are not supported on LoginHistory in every context, and results can be large. A practical approach is to export a period and count in a spreadsheet, or to query narrow windows and count the returned rows. If you have Event Monitoring, LoginEvent supports richer analysis.
Linking login history to MFA coverage
Login history shows what people did. To find who has not registered a verification method, use TwoFactorInfo, which holds one record for each user with a registered method.
SELECT Id, Name, Username
FROM User
WHERE IsActive = true
AND Id NOT IN (SELECT UserId FROM TwoFactorInfo)The result is your registration to-do list: active users with no recorded verification method. Combine the two views:
- Run the
TwoFactorInfoquery to list users without a method. - Run a
LoginHistoryquery for the same users to see whether they are signing in, and how. - Contact the active ones first, since they are the ones MFA will interrupt.
A user who appears in both lists and signs in by Application is the most exposed. A user who signs in only through SSO may be covered by your identity provider instead. The chapter on single sign-on explains that split.
Common mistakes
- Reading
LoginTimeas local time. The API returns UTC. The Setup page shows your own time zone. Compare like with like before you correlate events. - Forgetting the row limit. The Setup page shows a limited set of recent records. For a longer window, download the file or query by date range.
- Treating one failure as an attack. People mistype passwords. Look for repetition, many users from one address, or failures followed by a success.
- Ignoring integration logins. A service account that logs in all day makes the log noisy. Filter by
LoginTypeorApplicationso human activity stands out. - Trusting
SourceIpblindly. Users behind a VPN or corporate proxy share an address, and mobile users change addresses often. Use IP as one signal, not proof. - Reviewing only after an incident. The six-month window is a limit. Download and archive records if your compliance rules require a longer history.
Building a review routine
A routine turns these queries into a habit. A simple version looks like this:
| Frequency | Check | Action |
|---|---|---|
| Daily | Failures in the last 24 hours | Investigate repeated failures from one address or against one user |
| Weekly | Successful logins from addresses outside expected ranges | Confirm with the user, reset credentials if unexplained |
| Weekly | Active users missing from `TwoFactorInfo` | Send reminders, track progress |
| After any login setting change | Failures in the following day | Confirm the change did not lock out legitimate users |
When you find something suspicious, the usual response is to deactivate or freeze the user, end their sessions, reset the password, and check Setup Audit Trail for changes made from that account.
Summary
Login history records every sign-in attempt along with its type, address, and result. You can read it in Setup or query the LoginHistory object with SOQL, and you can pair it with TwoFactorInfo to see who still lacks a verification method. Read failures as signals about which control acted, watch for patterns instead of single events, and run the same checks on a schedule.
Exercise
A query of failed logins from the last day shows 40 Invalid Password failures. They come from one SourceIp and target 38 different usernames, with no successes. What is the most likely cause, and what is one step you would take?
Show answer
This pattern points to password spraying, where one source tries a common password against many accounts. It is unlikely to be 38 users forgetting passwords from the same address. One step: confirm the address is not your own network, then block it, for example by tightening login IP ranges on profiles, and check whether any of those usernames later logged in successfully. Also consider requiring MFA for those accounts.
Exercise
Write a SOQL query that lists the username, time, source IP, and login type for all logins in the past 14 days that were not successful, newest first.
Show answer
SELECT User.Username, LoginTime, SourceIp, LoginType FROM LoginHistory WHERE LoginTime = LASTNDAYS:14 AND Status != 'Success' ORDER BY LoginTime DESC
Exercise
Why is a user who signs in with LoginType of Application and has no record in TwoFactorInfo a higher priority than a user who signs in only through SAML SSO?
Show answer
The first user signs in on the standard Salesforce login page with only a username and password, so no second factor protects the account. The SSO user authenticates at the identity provider, which may enforce MFA itself, so Salesforce-side registration matters less for that user.