Skip to content

Commit 20307e6

Browse files
authored
Merge pull request #2 from Azure-Samples/adaptingToAzureADV2
Adapting to azure adv2
2 parents d1622e2 + bd3d894 commit 20307e6

File tree

6 files changed

+77
-24
lines changed

6 files changed

+77
-24
lines changed

README.md

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Next time a user runs the application, the user is signed-in with the same ident
4848
- Install .NET Core for Windows by following the instructions at [dot.net/core](https://dot.net/core), which will include [Visual Studio 2017](https://aka.ms/vsdownload).
4949
- An Internet connection
5050
- An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, see [How to get an Azure AD tenant](https://azure.microsoft.com/en-us/documentation/articles/active-directory-howto-tenant/)
51-
- A user account in your Azure AD tenant. This sample will not work with a Microsoft account (formerly Windows Live account). Therefore, if you signed in to the [Azure portal](https://portal.azure.com) with a Microsoft account and have never created a user account in your directory before, you need to do that now. (See [Quickstart: Add new users to Azure Active Directory](https://docs.microsoft.com/en-us/azure/active-directory/add-users-azure-active-directory)
51+
- A user account in your Azure AD tenant, or a Microsoft personal account
5252

5353
### Step 1: Clone or download this repository
5454

@@ -64,34 +64,68 @@ git clone https://github.com/Azure-Samples/active-directory-dotnet-native-aspnet
6464

6565
There are two projects in this sample. Each needs to be separately registered in your Azure AD tenant. To register these projects, you can:
6666

67-
#### Register the TodoListService web API
67+
#### Navigate to the Application registration portal
6868

69-
> TODO Update this
69+
Sign in in [apps.dev.microsoft.com/](apps.dev.microsoft.com/). From there, you can add converged applications
7070

71-
#### Register the TodoListClient app
71+
#### Register the TodoListClient-v2 app
7272

73-
> TODO Update this
73+
1. In the [application registration portal](apps.dev.microsoft.com), click **Add an app**
74+
1. In the *Register your application* page, provide a name for your application for instance `TodoListClient-v2`
75+
1. Press the **Create** button
76+
1. In the registration page for your application, copy the *application ID* to the clipboard you will need it to configure the code for your application
77+
1. Press the **Save** button at the bottom of the page.
78+
1. In the *Platforms* section, click on the **Add Platform** button and then on **Native application**
79+
1. Click on the My applications link at the top of the page to get back to the list of applications in the app registration portal
80+
81+
#### Register the TodoListService-v2 web API
82+
83+
1. In the [application registration portal](apps.dev.microsoft.com), click **Add an app**
84+
1. In the *Register your application* page, provide a name for your application for instance `TodoListService-v2`
85+
1. Press the **Create** button
86+
1. In the registration page for your application, copy the *application ID* to the clipboard you will need it to configure the code for your application
87+
1. In the *Platforms* section, click on the **Add Platform** button and then on **Web API**
88+
1. Copy the scope proposed by default to access your web api as a user. It's in the form ``api://<Application ID>/access_as_user``
89+
1. In the *Web API platform*, in the *Pre-authorized applications* section click on **Add application**
90+
1. In the *application ID* field, paste the application ID of the client application as pasted from the registration
91+
1. In the *Scope* field, click on the **Select** combo box and select the scope for this Web API `api://<Application ID>/access_as_user`
92+
1. Press the **Save** button at the bottom of the page.
7493

7594
### Step 3: Configure the sample to use your Azure AD tenant
7695

77-
#### Configure the TodoListService C# project
96+
#### Choose which users account to sign in
97+
98+
By default the sample is configured to enable users to sign in with any work and school accounts (AAD) or Microsoft Personal accounts (formerly live account).
99+
This is because `ida:Tenant` has the value of `common`.
100+
101+
##### Important note
78102

79-
> TODO Update this
103+
`common` is **not** a proper tenant. It's just a **convention** to express that the accepted tenants are any Work and School organizations, or Personal Microsoft account (consumer accounts).
104+
Accepted tenants can have the following values:
105+
106+
Value | Meaning
107+
----- | --------
108+
`common` | users can sign in with any Work and School account, or Microsoft Personal account
109+
`organizations` | users can sign in with any Work and School account
110+
`consumers` | users can sign in with a Microsoft Personal account
111+
a GUID or domain name | users can only sign in with an account for a specific organization described by its tenant ID (GUID) or domain name
112+
113+
#### Configure the TodoListService C# project
80114

81115
1. Open the solution in Visual Studio.
82-
2. In the TodoListService project, open the `appsettings.json` file.
83-
3. Find the `Domain` property and replace the value with your AAD tenant domain, for example, contoso.onmicrosoft.com.
84-
4. Find the `TenantId` property and replace the value with the Tenant ID you registered earlier,
85-
5. Find the `ClientId` property and replace the value with the Application ID (Client ID) property of the Service application, that you registered earlier.
116+
1. In the TodoListService project, open the `appsettings.json` file.
117+
1. Find the `ClientId` property and replace the value with the Application ID (Client ID) property of the Service application, that you registered earlier.
118+
1. [Optional] if you want to limit sign-in to users in your organization, also update
119+
- The `Domain` property, replacing the existing value with your AAD tenant domain, for example, contoso.onmicrosoft.com.
120+
- The `TenantId` property replacing the existing value with the Tenant ID.
86121

87122
#### Configure the TodoListClient C# project
88123

89124
1. In the TodoListClient project, open `App.config`.
90-
2. Find the app key `ida:Tenant` and replace the value with your AAD Tenant ID (GUID). Alternatively you can also use your AAD tenant Name (for example, contoso.onmicrosoft.com).
91-
3. Find the app key `ida:ClientId` and replace the value with the ApplicationID (Client ID) for the TodoListClient from the Azure portal.
92-
4. Find the app key `ida:RedirectUri` and replace the value with the Redirect URI for the TodoListClient from the Azure portal, for example `https://TodoListClient`.
93-
5. Find the app key `todo:TodoListResourceId` and replace the value with the ApplicationID (Client ID) of the Service application (a GUID)
94-
6. If you changed the default value, find the app key `todo:TodoListBaseAddress` and replace the value with the base address of the TodoListService project.
125+
1. Find the app key `ida:ClientId` and replace the value with the ApplicationID (Client ID) for the TodoListClient-v2 app copied from the app registration page.
126+
1. Find the app key `todo:TodoListScope` and replace the value with the scope of the TodoListService-v2 application copied from the app registration (of the form ``api://<Application ID of service>/access_as_user``)
127+
1. [Optional] If you want your application to work only in your organization (only in your tenant) you'll also need to Find the app key `ida:Tenant` and replace the value with your AAD Tenant ID (GUID). Alternatively you can also use your AAD tenant Name (for example, contoso.onmicrosoft.com)
128+
1. [Optional] If you changed the default URL for your service application, find the app key `todo:TodoListBaseAddress` and replace the value with the base address of the TodoListService project.
95129

96130
### Step 4: Run the sample
97131

@@ -170,15 +204,15 @@ namespace TodoListService.Controllers
170204
```
171205

172206
This code gets the todo list items associated with their owner, which is the identity of the user using the Web API. It also adds todo list items associated with the same user.
173-
There is no persistence as implementing token persistance on the service side would be beyond the scope of this sample
207+
There is no persistence as implementing token persistence on the service side would be beyond the scope of this sample
174208

175209
The code of the `Configure` method in `AzureAdServiceCollectionExtension` was also modified to accept tokens coming from the V2 endpoint:
176210

177211
```CSharp
178212
public void Configure(string name, JwtBearerOptions options)
179213
{
180214
options.Audience = _azureOptions.ClientId;
181-
options.Authority = $"{_azureOptions.Instance}common/v2.0/";
215+
options.Authority = $"{_azureOptions.Instance}{_azureOptions.Tenant}/v2.0/";
182216

183217
// Instead of using the default validation (validating against a single tenant, as we do in line of business apps),
184218
// we inject our own multitenant validation logic (which even accepts both V1 and V2 tokens)
@@ -296,4 +330,6 @@ To understand better how the client code acquires a token, see ADAL.NET's concep
296330

297331
### Other documentation / samples
298332

333+
This sample is for the Azure AD V2 enpoint the same as [Calling a ASP.NET Core Web API from a WPF application using Azure AD](https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore) which is for the Azure AD V1 endpoint.
334+
299335
For more information about how the protocols work in this scenario and other scenarios, see [Authentication Scenarios for Azure AD](https://go.microsoft.com/fwlink/?LinkId=394414).

TodoListClient/App.config

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
<!--
1010
ida:Tenant can be:
1111
- a proper tenant Id (a GUID) or tenant name (e.g. contoso.onmicrosoft.com) to sign-in only users of a given Azure AD tenant:
12-
- one of the following three words (which are not proper tenant, but conventions)
13-
- 'organizations' to sign-in Work or School accounts
12+
- one of the following three words (**which are not proper tenants**, but only conventions)
1413
- 'common' to sign-in Work or School accounts and Microsoft personal accounts
14+
- 'organizations' to sign-in Work or School accounts
1515
- 'consumers' to sign-in only Microsoft personal accounts
1616
-->
17-
<add key="ida:Tenant" value="[Enter the name of your tenant, e.g. contoso.onmicrosoft.com]" />
17+
<add key="ida:Tenant" value="common" />
1818
<add key="ida:ClientId" value="[Enter the Application ID (Client ID) of your application from the portal, e.g. b3682cc7-8b30-4bd2-aaba-080c6bf0fd31]" />
1919

2020
<!--

TodoListService/Extensions/AzureAdAuthenticationBuilderExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ public ConfigureAzureOptions(IOptions<AzureAdOptions> azureOptions)
3131
public void Configure(string name, JwtBearerOptions options)
3232
{
3333
options.Audience = _azureOptions.ClientId;
34-
options.Authority = $"{_azureOptions.Instance}common/v2.0/";
34+
options.Authority = $"{_azureOptions.Instance}{_azureOptions.TenantId}/v2.0/";
3535

3636
// Instead of using the default validation (validating against a single tenant, as we do in line of business apps),
3737
// we inject our own multitenant validation logic (which even accepts both V1 and V2 tokens)
3838
options.TokenValidationParameters.ValidateIssuer = true;
39+
40+
// If you want to use the V2 endpoint (that is authority = $"{_azureOptions.Instance}common/v2.0/")
41+
// you'd also want to validate which tenants your Web API accept
42+
// in that case you'd have to implement a IssuerValidator and uncomment the following line.
3943
options.TokenValidationParameters.IssuerValidator = ValidateIssuer;
4044
}
4145

TodoListService/TextFile.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Until V2 apps are present in Ibiza, the Web API needs to be provisionned
2+
See https://stackoverflow.com/questions/45631145/how-to-allow-non-admin-users-to-authenticate-via-oauth2-0-for-tenants-where-user/45643173#45643173
3+
4+
which translates in:
5+
https://login.windows.net/common/oauth2/authorize?response_type=code&client_id={0}&resource={1}&redirect_uri={2}&prompt=admin_consent
6+
where client id and resource= the client id of the web api
7+
8+
for instance:
9+
https://login.windows.net/common/oauth2/authorize?response_type=code&client_id=a88bb933-319c-41b5-9f04-eff36d985612&resource=a88bb933-319c-41b5-9f04-eff36d985612&redirect_uri=https://localhost:44351&prompt=admin_consent

TodoListService/TodoListService.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<Folder Include="wwwroot\" />
10+
<Compile Remove="wwwroot\**" />
11+
<Content Remove="wwwroot\**" />
12+
<EmbeddedResource Remove="wwwroot\**" />
13+
<None Remove="wwwroot\**" />
1114
</ItemGroup>
1215

1316
<ItemGroup>

TodoListService/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
Otherwise you can leave them blank
1010
*/
1111
"Domain": "qualified.domain.name", // for instance contoso.onmicrosoft.com
12-
"TenantId": "22222222-2222-2222-2222-222222222222"
12+
"TenantId": "common" // 'common' or 'organizations' or 'consumers'
13+
// or tenant ID like '22222222-2222-2222-2222-222222222222'
1314
},
1415
"Logging": {
1516
"IncludeScopes": false,

0 commit comments

Comments
 (0)