Skip to content

Commit 1eaad86

Browse files
committed
new code sample info
1 parent a1a0e91 commit 1eaad86

File tree

1 file changed

+40
-56
lines changed

1 file changed

+40
-56
lines changed

articles/active-directory-b2c/manage-user-accounts-graph-api.md

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,29 @@ Microsoft Graph API supports creating and updating a user with extension attribu
9898
## Code sample
9999

100100
This code sample is a .NET Core console application that uses the [Microsoft Graph SDK](https://docs.microsoft.com/graph/sdks/sdks-overview) to interact with Microsoft Graph API. Its code demonstrates how to call the API to programmatically manage users in an Azure AD B2C tenant.
101-
You can [download the sample archive](https://github.com/Azure-Samples) (*.zip), [browse the repository](https://github.com/Azure-Samples) on GitHub, or clone the repository:
101+
You can [download the sample archive](https://github.com/Azure-Samples/ms-identity-dotnetcore-b2c-account-management/archive/master.zip) (*.zip), [browse the repository](https://github.com/Azure-Samples/ms-identity-dotnetcore-b2c-account-management) on GitHub, or clone the repository:
102102

103103
```cmd
104-
git clone https://github.com/AzureADQuickStarts/B2C-GraphAPI-DotNet.git
104+
git clone https://github.com/Azure-Samples/ms-identity-dotnetcore-b2c-account-management.git
105105
```
106106

107107
After you've obtained the code sample, configure it for your environment and then build the project:
108108

109-
1. Open the solution in [Visual Studio](https://visualstudio.microsoft.com) or [Visual Studio Code](https://code.visualstudio.com).
110-
1. Open `appsettings.json`.
111-
1. Under the `appSettings` section, replace `{your-b2c-tenant}` with the name of your tenant, and `{Application ID}` and `{Client secret}` with the values for your management application registration (see the [Register a management application](#register-a-management-application) section of this article).
112-
1. The console application can be found in `bin\Debug\netcoreapp3.0` folder. To run the application, run following command:
109+
1. Open the project in [Visual Studio](https://visualstudio.microsoft.com) or [Visual Studio Code](https://code.visualstudio.com).
110+
1. Open `src/appsettings.json`.
111+
1. In the `appSettings` section, replace `your-b2c-tenant` with the name of your tenant, and `Application (client) ID` and `Client secret` with the values for your management application registration (see the [Register a management application](#register-a-management-application) section of this article).
112+
1. Open a console window within your local clone of the repo, switch into the `src` directory, then build the project:
113+
```console
114+
cd src
115+
dotnet build
116+
```
117+
1. Run the application with the `dotnet` command:
113118

114-
```cmd
115-
dotnet aad-b2c-graph.dll
119+
```console
120+
dotnet bin/Debug/netcoreapp3.0/b2c-ms-graph.dll
116121
```
117122

118-
The application will show you list of commands you can use. For example: List users, Get a user, delete user, update user's password, and create bulk users.
123+
The application displays a list of commands you can execute. For example, get all users, get a single user, delete a user, update a user's password, and bulk import.
119124

120125
### Code discussion
121126

@@ -125,71 +130,50 @@ Any request to the Microsoft Graph API requires an access token for authenticati
125130

126131
The `RunAsync` method in the _Program.cs_ file:
127132

128-
1. Reads application settings from the _appSettings.json_ file
133+
1. Reads application settings from the _appsettings.json_ file
129134
1. Initializes the auth provider using [OAuth 2.0 client credentials grant](../active-directory/develop/v2-oauth2-client-creds-grant-flow.md) flow. With the client credentials grant flow, the app is able to get an access token to call the Microsoft Graph API.
130-
1. Next, it sets up the Microsoft Graph service client with the auth provider:
135+
1. Sets up the Microsoft Graph service client with the auth provider:
131136

132137
```csharp
133-
// Read application settings
138+
// Read application settings from appsettings.json (tenant ID, app ID, client secret, etc.)
134139
AppSettings config = AppSettingsFile.ReadFromJsonFile();
135140

136-
// Initialize the auth provider with values from appsettings.json
141+
// Initialize the client credential auth provider
137142
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
138143
.Create(config.AppId)
139144
.WithTenantId(config.TenantId)
140-
.WithClientSecret(config.AppSecret)
145+
.WithClientSecret(config.ClientSecret)
141146
.Build();
142-
143-
// Setup the MS Graph service client with client credentials
144-
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
145147
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
148+
149+
// Set up the Microsoft Graph service client with client credentials
146150
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
147151
```
148152

149-
The Microsoft Graph SDK service libraries provide a client class that you can use as the starting point for creating all API requests. Learn how to [Make API calls using the Microsoft Graph SDKs](https://docs.microsoft.com/graph/sdks/create-requests).
153+
The initialized *GraphServiceClient* is then used in _UserService.cs_ to perform the user management operations. For example, getting a list of the user accounts in the tenant:
150154

151-
The _main_ method (in the _Program_ class) invokes the _GetUserBySignInName_ method in the _Services\\UserService.cs_ file. This method:
155+
```csharp
156+
public static async Task ListUsers(GraphServiceClient graphClient)
157+
{
158+
Console.WriteLine("Getting list of users...");
152159

153-
1. Accepted `AppSettings` and `GraphServiceClient` objects that have been initiated in the _Program_ class.
154-
1. Asks the user to provide the sign-in name
155-
1. Uses the `graphClient.Users` to find the account in the directory.
156-
1. Write the result to the console.
160+
// Get all users (one page)
161+
var result = await graphClient.Users
162+
.Request()
163+
.Select(e => new
164+
{
165+
e.DisplayName,
166+
e.Id,
167+
e.Identities
168+
})
169+
.GetAsync();
157170

158-
```csharp
159-
public static async Task GetUserBySignInName(AppSettings config, GraphServiceClient graphClient)
171+
foreach (var user in result.CurrentPage)
160172
{
161-
Console.WriteLine("Type user sign-in name (username or email address)");
162-
string userId = Console.ReadLine();
163-
164-
Console.WriteLine($"Looking for user with sign-in name '{userId}'...");
165-
166-
try
167-
{
168-
// Get user by object Id
169-
var result = await graphClient.Users
170-
.Request()
171-
.Filter($"identities/any(c:c/issuerAssignedId eq '{userId}' and c/issuer eq '{config.TenantId}')")
172-
.Select(e => new
173-
{
174-
e.DisplayName,
175-
e.Id,
176-
e.Identities
177-
})
178-
.GetAsync();
179-
180-
if (result != null)
181-
{
182-
Console.WriteLine(JsonConvert.SerializeObject(result));
183-
}
184-
}
185-
catch (Exception ex)
186-
{
187-
Console.ForegroundColor = ConsoleColor.Red;
188-
Console.WriteLine(ex.Message);
189-
Console.ResetColor();
190-
}
173+
Console.WriteLine(JsonConvert.SerializeObject(user));
191174
}
192-
```
175+
}
176+
```
193177

194178
[Make API calls using the Microsoft Graph SDKs](https://docs.microsoft.comgraph/sdks/create-requests) includes information on how to read and write information from Microsoft Graph, use `$select` to control the properties returned, provide custom query parameters, and use the `$filter` and `$orderBy` query parameters.
195179

0 commit comments

Comments
 (0)