Skip to content

Commit 53b3f17

Browse files
committed
Merge from complete
2 parents 29da03b + 9481a32 commit 53b3f17

21 files changed

Lines changed: 2853 additions & 1482 deletions

README.md

Lines changed: 72 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,56 +32,97 @@ You can clone this sample from your shell or command line:
3232

3333
### Step 2: Register your Web API - *TodoListService* in the *Application registration portal*
3434

35-
1. Sign in to the [Application registration portal](https://apps.dev.microsoft.com/portal/register-app) either using a personal Microsoft account (live.com or hotmail.com) or work or school account.
36-
1. Give a name to your Application, such as `AppModelv2-NativeClient-DotNet-TodoListService`. Make sure that the *Guided Setup* option is **Unchecked** then press **Create**. The portal will assign your app a globally unique *Application ID* that you'll use later in your code.
37-
1. Click **Add Platform**, and select **Web API**
38-
1. Click **Save**
39-
40-
> Note: When you add a *Web API* the Application registration portal, it adds a pre-defined App Id URI and Scope, using the format *api://{Application Id}/{Scope Name}* named **access_as_user** (you can review it by clicking 'Edit' button). This sample code uses this default scope.
41-
42-
### Step 3: Configure your *TodoListService* and *TodoListClient* projects to match the Web API you just registered
35+
#### Choose the Azure AD tenant where you want to create your applications
36+
37+
If you want to register your apps manually, as a first step you'll need to:
38+
39+
1. Sign in to the [Azure portal](https://portal.azure.com) using either a work or school account or a personal Microsoft account.
40+
1. If your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then **switch directory**.
41+
Change your portal session to the desired Azure AD tenant.
42+
43+
#### Register the service app (TodoListService)
44+
45+
1. Navigate to the Microsoft identity platform for developers [App registrations](https://go.microsoft.com/fwlink/?linkid=2083908) page.
46+
1. Select **New registration**.
47+
1. When the **Register an application page** appears, enter your application's registration information:
48+
- In the **Name** section, enter a meaningful application name that will be displayed to users of the app, for example `AppModelv2-NativeClient-DotNet-TodoListService`.
49+
- Change **Supported account types** to **Accounts in any organizational directory**.
50+
- Select **Register** to create the application.
51+
52+
1. On the app **Overview** page, find the **Application (client) ID** value and record it for later. You'll need it to configure the Visual Studio configuration file for this project (`ClientId` in `TodoListService\Web.config`).
53+
1. Select the **Expose an API** section, and:
54+
- Select **Add a scope**
55+
- accept the proposed Application ID URI (api://{clientId}) by selecting **Save and Continue**
56+
- Enter the following parameters:
57+
- for **Scope name** use `access_as_user`
58+
- Ensure the **Admins and users** option is selected for **Who can consent**
59+
- in **Admin consent display name** type `Access TodoListService as a user`
60+
- in **Admin consent description** type `Accesses the TodoListService Web API as a user`
61+
- in **User consent display name** type `Access TodoListService as a user`
62+
- in **User consent description** type `Accesses the TodoListService Web API as a user`
63+
- Keep **State** as **Enabled**
64+
- Select **Add scope**
65+
66+
#### Configure your *TodoListService* and *TodoListClient* projects to match the Web API you just registered
4367

4468
1. Open the solution in Visual Studio and then open the **Web.config** file under the root of **TodoListService** project.
45-
1. Replace the value of `ida:ClientId` parameter with the **Application Id** from the application you just registered in the Application Registration Portal.
69+
1. Replace the value of `ida:ClientId` parameter with the **Client ID (Application Id)** from the application you just registered in the Application Registration Portal.
70+
71+
#### Add the new scope to the *TodoListClient*`s app.config
4672

47-
#### Step 3.1: Add the new scope to the *TodoListClient*`s app.config
73+
1. Open the **app.config** file located in **TodoListClient** project's root folder and then paste **Application Id** from the application you just registered for your *TodoListService* under `TodoListServiceScope` parameter, replacing the string `{Enter the Application Id of your TodoListService from the app registration portal}`.
4874

49-
1. Open the **app.config** file located in **TodoListClient** project's root folder and then paste **Application Id** from the application you just registered for your *TodoListService* under `TodoListServiceScope` parameter, replacing the string `{Enter the Application Id of your TodoListService from the app registration portal}`.
75+
> Note: Make sure it uses the following format:
76+
>
77+
> `api://{TodoListService-Application-Id}/access_as_user`
78+
>
79+
>(where {TodoListService-Application-Id} is the Guid representing the Application Id for your TodoListService).
5080
51-
> Note: Make sure it uses has the format `api://{TodoListService-Application-Id}/access_as_user` (where {TodoListService-Application-Id} is the Guid representing the Application Id for your TodoListService).
81+
### Step 3: Register the client app (TodoListClient)
5282

53-
### Step 4: Register the *TodoListClient* application in the *Application registration portal*
83+
In this step, you configure your *TodoListClient* project by registering a new application in the Application registration portal. In the cases where the client and server are considered *the same application* you may also just reuse the same application registered in the 'Step 2.'. Using the same application is actually needed if you want users to sign-in with Microsoft personal accounts
5484

55-
In this step, you configure your *TodoListClient* project by registering a new application in the Application registration portal. In the cases where the client and server are considered *the same application* you may also just reuse the same application registered in the 'Step 2.'.
85+
#### Register the *TodoListClient* application in the *Application registration portal*
5686

57-
1. Go back to [Application registration portal](https://apps.dev.microsoft.com/portal/register-app) to register a new application
58-
1. Give a name to your Application, such as `NativeClient-DotNet-TodoListClient`, make sure that the *Guided Setup* option is **Unchecked** then press **Create**.
59-
1. Click **Add Platform**, and select **Native**.
60-
1. Click **Save**
87+
1. Navigate to the Microsoft identity platform for developers [App registrations](https://go.microsoft.com/fwlink/?linkid=2083908) page.
88+
1. Select **New registration**.
89+
1. When the **Register an application page** appears, enter your application's registration information:
90+
- In the **Name** section, enter a meaningful application name that will be displayed to users of the app, for example `NativeClient-DotNet-TodoListClient`.
91+
- Change **Supported account types** to **Accounts in any organizational directory**.
92+
- Select **Register** to create the application.
93+
1. From the app's Overview page, select the **Authentication** section.
94+
- In the **Redirect URLs** | **Suggested Redirect URLs for public clients (mobile, desktop)** section, check **urn:ietf:wg:oauth:2.0:oob**
95+
- Select **Save**.
96+
1. Select the **API permissions** section
97+
- Click the **Add a permission** button and then,
98+
- Select the **My APIs** tab.
99+
- In the list of APIs, select the `AppModelv2-NativeClient-DotNet-TodoListService API`, or the name you entered for the Web API.
100+
- Check the **access_as_user** permission if it's not already checked. Use the search box if necessary.
101+
- Select the **Add permissions** button
61102

62-
### Step 5: Configure your *TodoListClient* project
103+
#### Configure your *TodoListClient* project
63104

64-
1. In the *Application registration portal*, copy the value of the **Application Id**
105+
1. In the *Application registration portal*, in the **Overview** page copy the value of the **Application (client) Id**
65106
1. Open the **app.config** file located in the **TodoListClient** project's root folder and then paste the value in the `ida:ClientId` parameter value
66107

67-
### Step 6: Run your project
108+
### Step 4: Run your project
68109

69110
1. Press `<F5>` to run your project. Your *TodoListClient* should open.
70-
1. Select **Sign in** in the top right and sign in with the same user you have used to register your aplication, or a user in the same directory.
111+
1. Select **Sign in** in the top right and sign in with the same user you have used to register your application, or a user in the same directory.
71112
1. At this point, if you are signing in for the first time, you may be prompted to consent to *TodoListService* Web Api.
72113
1. The sign-in also request the access token to the *access_as_user* scope to access *TodoListService* Web Api and manipulate the *To-Do* list.
73114

74-
### Step 7: Pre-authorize your client application
115+
### Step 5: Pre-authorize your client application
75116

76-
One of the ways to allow users from other directories to acces your Web API is by *pre-authorizing* the client applications to access your Web API by adding the Application Ids from client applications in the list of *pre-authorized* applications for your Web API. By adding a pre-authorized client, you will not require user to consent to use your Web API. Follow the steps below to pre-authorize your Web Application::
117+
One of the ways to allow users from other directories to access your Web API is by *pre-authorizing* the client applications to access your Web API by adding the Application Ids from client applications in the list of *pre-authorized* applications for your Web API. By adding a pre-authorized client, you will not require user to consent to use your Web API. Follow the steps below to pre-authorize your Web Application::
77118

78119
1. Go back to the *Application registration portal* and open the properties of your **TodoListService**.
79-
1. In the **Web API platform**, click on **Add application** under the *Pre-authorized applications* section.
80-
1. In the *Application ID* field, paste the application ID of the `TodoListClient` application.
81-
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`.
82-
1. Press the **Save** button at the bottom of the page.
120+
1. In the **Expose an API** section, click on **Add a client application** under the *Authorized client applications* section.
121+
1. In the *Client ID* field, paste the application ID of the `TodoListClient` application.
122+
1. In the *Authorized scopes* section, select the scope for this Web API `api://<Application ID>/access_as_user`.
123+
1. Press the **Add application** button at the bottom of the page.
83124

84-
### Step 8: Run your project
125+
### Step 6: Run your project
85126

86127
1. Press `<F5>` to run your project. Your *TodoListClient* should open.
87128
1. Select **Sign in** in the top right (or Clear Cache/Sign-in) and then sign-in either using a personal Microsoft account (live.com or hotmail.com) or work or school account.
@@ -96,8 +137,8 @@ To restrict who can sign in to your application, use one of the options:
96137

97138
You can restrict sign-in access for your application to only user accounts that are in a single Azure AD tenant - including *guest accounts* of that tenant. This scenario is a common for *line-of-business applications*:
98139

99-
1. In the **web.config** file of your **TodoListService**, change the value for the `Tenant` parameter from `Common` to the tenant name of the organization, such as `contoso.onmicrosoft.com` or the *Tenant Id*.
100-
2. Open **App_Start\Startup.Auth** file and set the `ValidateIssuer` argument to `true`.
140+
1. Open the **App_Start\Startup.Auth** file, and change the value of the metadata endpoint that's passed into the `OpenIdConnectSecurityTokenProvider` to `"https://login.microsoftonline.com/{Tenant ID}/v2.0/.well-known/openid-configuration"` (you can also use the Tenant Name, such as `contoso.onmicrosoft.com`).
141+
2. In the same file, set the `ValidIssuer` property on the `TokenValidationParameters` to `"https://sts.windows.net/{Tenant Id}/"` and the `ValidateIssuer` argument to `true`.
101142

102143
#### Option 2: Use a custom method to validate issuers
103144

TodoListClient/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
clientId of a legacy application, created in the https://portal.azure.com portal.
3434
-->
3535

36-
<add key="TodoListServiceScope" value="{Enter the Application Id of your TodoListService from the app registration portal}" />
36+
<add key="TodoListServiceScope" value="api://{Enter the Application Id of your TodoListService from the app registration portal}/access_as_user" />
3737
<add key="TodoListServiceBaseAddress" value="https://localhost:44321/" />
3838

3939
</appSettings>

TodoListClient/TokenCacheHelper.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
2-
// Licensed under the MIT License.
1+
//------------------------------------------------------------------------------
2+
//
3+
// Copyright (c) Microsoft Corporation.
4+
// All rights reserved.
5+
//
6+
// This code is licensed under the MIT License.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files(the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions :
14+
//
15+
// The above copyright notice and this permission notice shall be included in
16+
// all copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
// THE SOFTWARE.
25+
//
26+
//------------------------------------------------------------------------------
327

428
using System.IO;
529
using System.Security.Cryptography;
@@ -8,7 +32,8 @@
832
namespace TodoListClient
933
{
1034
static class TokenCacheHelper
11-
{
35+
{
36+
1237
/// <summary>
1338
/// Path to the token cache
1439
/// </summary>

TodoListService/App_Start/Startup.Auth.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@ public partial class Startup
1717
public void ConfigureAuth(IAppBuilder app)
1818
{
1919
// NOTE: The usual WindowsAzureActiveDirectoryBearerAuthentication middleware uses a
20-
// metadata endpoint which is not supported by the v2.0 endpoint. Instead, this
20+
// metadata endpoint which is not supported by the Microsoft identity platform endpoint. Instead, this
2121
// OpenIdConnectSecurityTokenProvider implementation can be used to fetch & use the OpenIdConnect
22-
// metadata document - which for the v2 endpoint is https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration
22+
// metadata document - which for the identity platform endpoint is https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration
2323

2424
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
2525
{
2626
AccessTokenFormat = new JwtFormat(
2727
new TokenValidationParameters
2828
{
2929
// Check if the audience is intended to be this application
30-
ValidAudience = clientId,
30+
ValidAudiences = new [] { clientId, $"api://{clientId}" },
3131

3232
// Change below to 'true' if you want this Web API to accept tokens issued to one Azure AD tenant only (single-tenant)
33+
// Note that this is a simplification for the quickstart here. You should validate the issuer. For details,
34+
// see https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore
3335
ValidateIssuer = false,
3436

3537
},

0 commit comments

Comments
 (0)