|
1 | 1 | ---
|
2 |
| -title: Access and review audit logs |
3 |
| -titleSuffix: Azure AD B2C |
4 |
| -description: How to access Azure AD B2C audit logs programmatically and in the Azure portal. |
5 |
| -services: active-directory-b2c |
6 |
| -author: mmacy |
7 |
| -manager: celestedg |
8 |
| - |
9 |
| -ms.service: active-directory |
10 |
| -ms.topic: conceptual |
11 |
| -ms.workload: identity |
12 |
| -ms.date: 10/16/2019 |
13 |
| -ms.author: marsma |
14 |
| -ms.subservice: B2C |
15 |
| -ms.custom: fasttrack-edit |
| 2 | +redirect_url: /azure/active-directory-b2c/view-audit-logs |
| 3 | +redirect_document_id: true |
16 | 4 | ---
|
17 |
| - |
18 |
| -# Accessing Azure AD B2C audit logs |
19 |
| - |
20 |
| -Azure Active Directory B2C (Azure AD B2C) emits audit logs containing activity information about B2C resources, tokens issued, and administrator access. This article provides a brief overview of the information available in audit logs and instructions on how to access this data for your Azure AD B2C tenant. |
21 |
| - |
22 |
| -Audit log events are only retained for **seven days**. Plan to download and store your logs using one of the methods shown below if you require a longer retention period. |
23 |
| - |
24 |
| -> [!NOTE] |
25 |
| -> You can't see user sign-ins for individual Azure AD B2C applications under the **Users** section of the **Azure Active Directory** or **Azure AD B2C** pages in the Azure portal. The sign-in events there show user activity, but can't be correlated back to the B2C application that the user signed in to. You must use the audit logs for that, as explained further in this article. |
26 |
| -
|
27 |
| -## Overview of activities available in the B2C category of audit logs |
28 |
| - |
29 |
| -The **B2C** category in audit logs contains the following types of activities: |
30 |
| - |
31 |
| -|Activity type |Description | |
32 |
| -|---------|---------| |
33 |
| -|Authorization |Activities concerning the authorization of a user to access B2C resources (for example, an administrator accessing a list of B2C policies). | |
34 |
| -|Directory |Activities related to directory attributes retrieved when an administrator signs in using the Azure portal. | |
35 |
| -|Application | Create, read, update, and delete (CRUD) operations on B2C applications. | |
36 |
| -|Key |CRUD operations on keys stored in a B2C key container. | |
37 |
| -|Resource |CRUD operations on B2C resources. For example, policies and identity providers. |
38 |
| -|Authentication |Validation of user credentials and token issuance.| |
39 |
| - |
40 |
| -For user object CRUD activities, refer to the **Core Directory** category. |
41 |
| - |
42 |
| -## Example activity |
43 |
| - |
44 |
| -This example image from the Azure portal shows the data captured when a user signs in with an external identity provider, in this case, Facebook: |
45 |
| - |
46 |
| - |
47 |
| - |
48 |
| -The activity details panel contains the following relevant information: |
49 |
| - |
50 |
| -|Section|Field|Description| |
51 |
| -|-------|-----|-----------| |
52 |
| -| Activity | Name | Which activity took place. For example, *Issue an id_token to the application*, which concludes the actual user sign-in. | |
53 |
| -| Initiated By (Actor) | ObjectId | The **Object ID** of the B2C application that the user is signing in to. This identifier is not visible in the Azure portal, but is accessible via the Microsoft Graph API. | |
54 |
| -| Initiated By (Actor) | Spn | The **Application ID** of the B2C application that the user is signing in to. | |
55 |
| -| Target(s) | ObjectId | The **Object ID** of the user that is signing in. | |
56 |
| -| Additional Details | TenantId | The **Tenant ID** of the Azure AD B2C tenant. | |
57 |
| -| Additional Details | PolicyId | The **Policy ID** of the user flow (policy) being used to sign the user in. | |
58 |
| -| Additional Details | ApplicationId | The **Application ID** of the B2C application that the user is signing in to. | |
59 |
| - |
60 |
| -## View audit logs in the Azure portal |
61 |
| - |
62 |
| -The Azure portal provides access to the audit log events in your Azure AD B2C tenant. |
63 |
| - |
64 |
| -1. Sign in to the [Azure portal](https://portal.azure.com) |
65 |
| -1. Switch to the directory that contains your Azure AD B2C tenant, and then browse to **Azure AD B2C**. |
66 |
| -1. Under **Activities** in the left menu, select **Audit logs**. |
67 |
| - |
68 |
| -A list of activity events logged over the last seven days is displayed. |
69 |
| - |
70 |
| - |
71 |
| - |
72 |
| -Several filtering options are available, including: |
73 |
| - |
74 |
| -* **Activity Resource Type** - Filter by the activity types shown in the table in the [Overview of activities available](#overview-of-activities-available-in-the-b2c-category-of-audit-logs) section. |
75 |
| -* **Date** - Filter the date range of the activities shown. |
76 |
| - |
77 |
| -If you select a row in the list, the activity details for the event are displayed. |
78 |
| - |
79 |
| -To download the list of activity events in a comma-separated values (CSV) file, select **Download**. |
80 |
| - |
81 |
| -## Get audit logs with the Azure AD reporting API |
82 |
| - |
83 |
| -Audit logs are published to the same pipeline as other activities for Azure Active Directory, so they can be accessed through the [Azure Active Directory reporting API](https://docs.microsoft.com/graph/api/directoryaudit-list). For more information, see [Get started with the Azure Active Directory reporting API](../active-directory/reports-monitoring/concept-reporting-api.md). |
84 |
| - |
85 |
| -### Enable reporting API access |
86 |
| - |
87 |
| -To allow script- or application-based access to the Azure AD reporting API, you need an Azure Active Directory application registered in your Azure AD B2C tenant with the following API permissions: |
88 |
| - |
89 |
| -* Microsoft Graph > Application permissions > AuditLog.Read.All |
90 |
| - |
91 |
| -You can enable these permissions on an existing Azure Active Directory application registration within your B2C tenant, or create a new one specifically for use with audit log automation. |
92 |
| - |
93 |
| -Follow these steps register an application, grant it the required Microsoft Graph API permissions, and then create a client secret. |
94 |
| - |
95 |
| -### Register application in Azure Active Directory |
96 |
| - |
97 |
| -[!INCLUDE [active-directory-b2c-appreg-mgmt](../../includes/active-directory-b2c-appreg-mgmt.md)] |
98 |
| - |
99 |
| -### Assign API access permissions |
100 |
| - |
101 |
| -#### [Applications](#tab/applications/) |
102 |
| - |
103 |
| -1. On the **Registered app** overview page, select **Settings**. |
104 |
| -1. Under **API ACCESS**, select **Required permissions**. |
105 |
| -1. Select **Add**, and then **Select an API**. |
106 |
| -1. Select **Microsoft Graph**, and then **Select**. |
107 |
| -1. Under **APPLICATION PERMISSIONS**, select **Read all audit log data**. |
108 |
| -1. Select the **Select** button, and then select **Done**. |
109 |
| -1. Select **Grant permissions**, and then select **Yes**. |
110 |
| - |
111 |
| -#### [App registrations (Preview)](#tab/app-reg-preview/) |
112 |
| - |
113 |
| -1. Under **Manage**, select **API permissions**. |
114 |
| -1. Under **Configured permissions**, select **Add a permission**. |
115 |
| -1. Select the **Microsoft APIs** tab. |
116 |
| -1. Select **Microsoft Graph**. |
117 |
| -1. Select **Application permissions**. |
118 |
| -1. Expand **AuditLog** and then select the **AuditLog.Read.All** check box. |
119 |
| -1. Select **Add permissions**. As directed, wait a few minutes before proceeding to the next step. |
120 |
| -1. Select **Grant admin consent for (your tenant name)**. |
121 |
| -1. Select your currently signed-in account if it's been assigned the *Global Administrator* role, or sign in with an account in your Azure AD B2C tenant that's been assigned the *Global Administrator* role. |
122 |
| -1. Select **Accept**. |
123 |
| -1. Select **Refresh**, and then verify that "Granted for ..." appears under **Status** for the *AuditLog.Read.All* permission. It might take a few minutes for the permissions to propagate. |
124 |
| - |
125 |
| -* * * |
126 |
| - |
127 |
| -### Create client secret |
128 |
| - |
129 |
| -[!INCLUDE [active-directory-b2c-client-secret](../../includes/active-directory-b2c-client-secret.md)] |
130 |
| - |
131 |
| -You now have an application with the required API access, an application ID, and a key that you can use in your automation scripts. See the PowerShell script section later in this article for an example of how you can get activity events with a script. |
132 |
| - |
133 |
| -### Access the API |
134 |
| - |
135 |
| -To download Azure AD B2C audit log events via the API, filter the logs on the `B2C` category. To filter by category, use the `filter` query string parameter when you call the Azure AD reporting API endpoint. |
136 |
| - |
137 |
| -```HTTP |
138 |
| -https://graph.microsoft.com/v1.0/auditLogs/directoryAudits?$filter=loggedByService eq 'B2C' and activityDateTime gt 2019-09-10T02:28:17Z |
139 |
| -``` |
140 |
| - |
141 |
| -### PowerShell script |
142 |
| - |
143 |
| -The following PowerShell script shows an example of how to query the Azure AD reporting API. After querying the API, it prints the logged events to standard output, then writes the JSON output to a file. |
144 |
| - |
145 |
| -You can try this script in the [Azure Cloud Shell](../cloud-shell/overview.md). Be sure to update it with your application ID, client secret, and the name of your Azure AD B2C tenant. |
146 |
| - |
147 |
| -```powershell |
148 |
| -# This script requires the registration of a Web Application in Azure Active Directory: |
149 |
| -# https://docs.microsoft.com/azure/active-directory/reports-monitoring/concept-reporting-api |
150 |
| -
|
151 |
| -# Constants |
152 |
| -$ClientID = "your-client-application-id-here" # Insert your application's client ID, a GUID (registered by Global Admin) |
153 |
| -$ClientSecret = "your-client-application-secret-here" # Insert your application's client secret |
154 |
| -$tenantdomain = "your-b2c-tenant.onmicrosoft.com" # Insert your Azure AD B2C tenant; for example, contoso.onmicrosoft.com |
155 |
| -$loginURL = "https://login.microsoftonline.com" |
156 |
| -$resource = "https://graph.microsoft.com" # Microsoft Graph API resource URI |
157 |
| -$7daysago = "{0:s}" -f (get-date).AddDays(-7) + "Z" # Use 'AddMinutes(-5)' to decrement minutes, for example |
158 |
| -Write-Output "Searching for events starting $7daysago" |
159 |
| -
|
160 |
| -# Create HTTP header, get an OAuth2 access token based on client id, secret and tenant domain |
161 |
| -$body = @{grant_type="client_credentials";resource=$resource;client_id=$ClientID;client_secret=$ClientSecret} |
162 |
| -$oauth = Invoke-RestMethod -Method Post -Uri $loginURL/$tenantdomain/oauth2/token?api-version=1.0 -Body $body |
163 |
| -
|
164 |
| -# Parse audit report items, save output to file(s): auditX.json, where X = 0 through n for number of nextLink pages |
165 |
| -if ($oauth.access_token -ne $null) { |
166 |
| - $i=0 |
167 |
| - $headerParams = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"} |
168 |
| - $url = "https://graph.microsoft.com/v1.0/auditLogs/directoryAudits?`$filter=loggedByService eq 'B2C' and activityDateTime gt " + $7daysago |
169 |
| -
|
170 |
| - # loop through each query page (1 through n) |
171 |
| - Do { |
172 |
| - # display each event on the console window |
173 |
| - Write-Output "Fetching data using Uri: $url" |
174 |
| - $myReport = (Invoke-WebRequest -UseBasicParsing -Headers $headerParams -Uri $url) |
175 |
| - foreach ($event in ($myReport.Content | ConvertFrom-Json).value) { |
176 |
| - Write-Output ($event | ConvertTo-Json) |
177 |
| - } |
178 |
| -
|
179 |
| - # save the query page to an output file |
180 |
| - Write-Output "Save the output to a file audit$i.json" |
181 |
| - $myReport.Content | Out-File -FilePath audit$i.json -Force |
182 |
| - $url = ($myReport.Content | ConvertFrom-Json).'@odata.nextLink' |
183 |
| - $i = $i+1 |
184 |
| - } while($url -ne $null) |
185 |
| -} else { |
186 |
| - Write-Host "ERROR: No Access Token" |
187 |
| -} |
188 |
| -``` |
189 |
| - |
190 |
| -Here's the JSON representation of the example activity event shown earlier in the article: |
191 |
| - |
192 |
| -```JSON |
193 |
| -{ |
194 |
| - "id": "B2C_DQO3J_4984536", |
195 |
| - "category": "Authentication", |
196 |
| - "correlationId": "00000000-0000-0000-0000-000000000000", |
197 |
| - "result": "success", |
198 |
| - "resultReason": "N/A", |
199 |
| - "activityDisplayName": "Issue an id_token to the application", |
200 |
| - "activityDateTime": "2019-09-14T18:13:17.0618117Z", |
201 |
| - "loggedByService": "B2C", |
202 |
| - "operationType": "", |
203 |
| - "initiatedBy": { |
204 |
| - "user": null, |
205 |
| - "app": { |
206 |
| - "appId": "00000000-0000-0000-0000-000000000000", |
207 |
| - "displayName": null, |
208 |
| - "servicePrincipalId": null, |
209 |
| - "servicePrincipalName": "00000000-0000-0000-0000-000000000000" |
210 |
| - } |
211 |
| - }, |
212 |
| - "targetResources": [ |
213 |
| - { |
214 |
| - "id": "00000000-0000-0000-0000-000000000000", |
215 |
| - "displayName": null, |
216 |
| - "type": "User", |
217 |
| - "userPrincipalName": null, |
218 |
| - "groupType": null, |
219 |
| - "modifiedProperties": [] |
220 |
| - } |
221 |
| - ], |
222 |
| - "additionalDetails": [ |
223 |
| - { |
224 |
| - "key": "TenantId", |
225 |
| - "value": "test.onmicrosoft.com" |
226 |
| - }, |
227 |
| - { |
228 |
| - "key": "PolicyId", |
229 |
| - "value": "B2C_1A_signup_signin" |
230 |
| - }, |
231 |
| - { |
232 |
| - "key": "ApplicationId", |
233 |
| - "value": "00000000-0000-0000-0000-000000000000" |
234 |
| - }, |
235 |
| - { |
236 |
| - "key": "Client", |
237 |
| - "value": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" |
238 |
| - }, |
239 |
| - { |
240 |
| - "key": "IdentityProviderName", |
241 |
| - "value": "facebook" |
242 |
| - }, |
243 |
| - { |
244 |
| - "key": "IdentityProviderApplicationId", |
245 |
| - "value": "0000000000000000" |
246 |
| - }, |
247 |
| - { |
248 |
| - "key": "ClientIpAddress", |
249 |
| - "value": "127.0.0.1" |
250 |
| - } |
251 |
| - ] |
252 |
| -} |
253 |
| -``` |
254 |
| - |
255 |
| -## Next steps |
256 |
| - |
257 |
| -You can automate other administration tasks, for example, [manage users with .NET](manage-user-accounts-graph-api.md). |
0 commit comments