Sub topic 11
Reviewing login history
This chapter assumes you can navigate Setup, know what a profile and a permission set are, and can read a basic SOQL query. By the end, you will be able to review successful and failed logins in the Setup UI and with queries. You will also be able to spot patterns that suggest attacks or MFA gaps.
What login history records
Salesforce records every login attempt in the LoginHistory object. Each row holds the user, the time, the login type, the source IP address, the application used, and the status. A failed attempt is recorded with a reason such as an invalid password or a locked account.
Salesforce keeps this data for six months. In Setup, go to Identity | Login History to view the last 20,000 records or download the full six months as a CSV or GZIP file. For anything longer, export the data on a schedule.
Key fields
| Field | What it tells you |
|---|---|
| `UserId` | Who attempted the login |
| `LoginTime` | When the attempt happened |
| `LoginType` | How the user signed in, such as application, SAML single sign-on, or OAuth |
| `SourceIp` | Where the request came from |
| `Status` | `Success` or the failure reason |
| `Application` | The client, such as a browser or an API tool |
Querying login history
The Setup page is fine for a quick look. SOQL lets you filter and combine results. This query lists the most recent logins from the last seven days.
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 or any SOQL tool. To focus on problems, add a condition such as AND Status != 'Success'. A cluster of failures from one SourceIp, or many failures against one user, points to password guessing.
Finding users who have not registered
Login history shows what happened. You also need to know who is not yet protected. This query returns active users with no verification method recorded in TwoFactorInfo.
SELECT Id, Name, Username
FROM User
WHERE IsActive = true
AND Id NOT IN (SELECT UserId FROM TwoFactorInfo)Use this list to target reminders before you require MFA. It also shows who may be locked out once enforcement starts.
A review routine
Look for these signals when you review:
- Repeated failures for one user, or one IP address failing across many users.
- Successful logins from countries where you have no staff.
- Logins outside the hours you set in the profile's login hours.
- Logins with a
LoginTypeof API or OAuth for users who normally use the browser. - Success right after a run of failures, which can mean a guessed password.
When you find something suspicious, reset the password, confirm the user's verification methods, and consider tightening login IP ranges for that profile.
Common errors
- Reading
LoginHistoryas a complete audit trail. It records logins only. Actions after login appear in other logs, such as the Setup Audit Trail. - Expecting data beyond six months. Older rows are gone. Export on a schedule if your policy requires a longer record.
- Treating one IP as one person. Offices and VPNs share addresses, and mobile users change them. Judge by pattern, not a single row.
- Forgetting access. Viewing login history needs the Manage Users permission. Querying
TwoFactorInfoalso needs permission to see that data, so a missing result can be an access problem. - Using
Status = 'Failed'. Failures carry descriptive text, not one fixed value. Filter withStatus != 'Success'.
Exercise
Your security team reports many failed logins from one IP address across dozens of usernames. Which query change helps you confirm this in LoginHistory, and what does the pattern suggest?
Show answer
Add AND Status != 'Success' to the login history query and review the SourceIp and UserId columns, or group the results by SourceIp. Many failures from one IP across many users suggests a password-spraying or credential-guessing attempt. Respond by checking whether any attempts succeeded, resetting affected passwords, and restricting login IP ranges where possible.
Exercise
Why run the TwoFactorInfo query before you enforce MFA instead of after?
Show answer
It lists active users with no registered verification method. Contacting them first lets you help them register before enforcement, which reduces lockouts and support requests.