You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 14, 2019. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+139Lines changed: 139 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,3 +2,142 @@ NativeClient-DotNet
2
2
===================
3
3
4
4
This sample demonstrates a .Net WPF application calling a web API that is secured using Azure AD. The .Net application uses the Active Directory Authentication Library (ADAL) to obtain a JWT access token through the OAuth 2.0 protocol. The access token is sent to the web API to authenticate the user.
5
+
6
+
For more information about how the protocols work in this scenario and other scenarios, see the [Authentication Scenarios for Azure AD](http://msdn.microsoft.com/aad) document.
7
+
8
+
## How To Run This Sample
9
+
10
+
To run this sample you will need:
11
+
- Visual Studio 2013
12
+
- An Internet connection
13
+
- An Azure subscription (a free trial is sufficient)
14
+
15
+
Every Azure subscription has an associated Azure Active Directory tenant. If you don't already have an Azure subscription, you can get a free subscription by signing up at [http://wwww.windowsazure.com](http://www.windowsazure.com). All of the Azure AD features used by this sample are available free of charge.
### Step 2: Create a user account in your Azure Active Directory tenant
24
+
25
+
If you already have a user account in your Azure Active Directory tenant, you can skip to the next step. This sample will not work with a Microsoft account, so if you signed in to the Azure portal with a Microsoft account and have never created a user account in your directory before, you need to do that now. If you create an account and want to use it to sign-in to the Azure portal, don't forget to add the user account as a co-administrator of your Azure subscription.
26
+
27
+
### Step 3: Register the sample with your Azure Active Directory tenant
28
+
29
+
There are two projects in this sample. Each needs to be separately registered in your Azure AD tenant.
30
+
31
+
#### Register the TodoListService web API
32
+
33
+
1. Sign in to the [Azure management portal](https://manage.windowsazure.com).
34
+
2. Click on Active Directory in the left hand nav.
35
+
3. Click the directory tenant where you wish to register the sample application.
36
+
4. Click the Applications tab.
37
+
5. In the drawer, click Add.
38
+
6. Click "Add an application my organization is developing".
39
+
7. Enter a friendly name for the application, for example "TodoListService", select "Web Application and/or Web API", and click next.
40
+
8. For the sign-on URL, enter the base URL for the sample, which is by default `https://localhost:44321`.
41
+
9. For the App ID URI, enter `https://<your_tenant_name>/TodoListService`, replacing `<your_tenant_name>` with the name of your Azure AD tenant. Click OK to complete the registration.
42
+
10. While still in the Azure portal, click the Configure tab of your application.
43
+
11. Find the Client ID value and copy it aside, you will need this later when configuring your application.
44
+
12. Using the Manage Manifest button in the drawer, download the manifest file for the application.
45
+
13. Add a permission to the application by replacing the appPermissions section with the block of JSON below. You will need to create a new GUID and replace the example permissionId GUID.
46
+
14. Using the Manage Manfiest button, upload the updated manifest file. Save the configuration of the app.
47
+
48
+
```JSON
49
+
"appPermissions": [
50
+
{
51
+
"claimValue": "user_impersonation",
52
+
"description": "Allow full access to the To Do List service on behalf of the signed-in user",
53
+
"directAccessGrantTypes": [],
54
+
"displayName": "Have full access to the To Do List service",
"userConsentDescription": "Allow full access to the To Do service on your behalf",
66
+
"userConsentDisplayName": "Have full access to the To Do service"
67
+
}
68
+
],
69
+
```
70
+
71
+
#### Register the TodoListClient app
72
+
73
+
1. Sign in to the [Azure management portal](https://manage.windowsazure.com).
74
+
2. Click on Active Directory in the left hand nav.
75
+
3. Click the directory tenant where you wish to register the sample application.
76
+
4. Click the Applications tab.
77
+
5. In the drawer, click Add.
78
+
6. Click "Add an application my organization is developing".
79
+
7. Enter a friendly name for the application, for example "TodoListClient-DotNet", select "Native Client Application", and click next.
80
+
8. For the Redirect URI, enter `http://TodoListClient`. Click finish.
81
+
9. Click the Configure tab of the application.
82
+
10. Find the Client ID value and copy it aside, you will need this later when configuring your application.
83
+
11. In "Permissions to Other Applications", select the TodoListService, and request the delegated permission "Have full access to the To Do List service". Save the configuration.
84
+
85
+
### Step 4: Configure the sample to use your Azure AD tenant
86
+
87
+
#### Configure the TodoListService project
88
+
89
+
1. Open the solution in Visual Studio 2013.
90
+
2. Open the `web.config` file.
91
+
3. Find the app key `ida:Tenant` and replace the value with your AAD tenant name.
92
+
4. Find the app key `ida:Audience` and replace the value with the App ID URI you registered earlier, for example `https://<your_tenant_name>/TodoListService`.
93
+
5. Find the app key `ida:ClientId` and replace the value with the Client ID for the TodoListService from the Azure portal.
94
+
95
+
#### Configure the TodoListClient project
96
+
97
+
1. Open `MainPage.xaml.cs'.
98
+
2. Find the declaration of `tenant` and replace the value with the name of your Azure AD tenant.
99
+
3. Find the declaration of `clientId` and replace the value with the Client ID from the Azure portal.
100
+
4. Find the declaration of `todoListResourceId` and `todoListBaseAddress` and ensure their values are set properly for your TodoListService project.
101
+
102
+
### Step 5: Trust the IIS Express SSL certificate
103
+
104
+
Since the web API is SSL protected, the client of the API (the web app) will refuse the SSL connection to the web API unless it trusts the API's SSL certificate.
105
+
106
+
Coming soon.
107
+
108
+
### Step 6: Run the sample
109
+
110
+
You know what to do! Explore the sample by signing in, adding items to the To Do list, removing the user account, and starting again. Notice that if you stop the application without removing the user account, the next time you run the application you won't be prompted to sign-in again - that is the sample implements a persistent cache for ADAL, and remembers the tokens from the previous run.
111
+
112
+
## How To Deploy This Sample to Azure
113
+
114
+
Coming soon.
115
+
116
+
## About The Code
117
+
118
+
Coming soon.
119
+
120
+
## How To Recreate This Sample
121
+
122
+
First, in Visual Studio 2013 create an empty solution to host the projects. Then, follow these steps to create each project.
123
+
124
+
### Creating the TodoListService Project
125
+
126
+
1. In the solution, create a new ASP.Net MVC web API project called TodoListService and while creating the project, click the Change Authentication button, select Organizational Accounts, Cloud - Single Organization, enter the name of your Azure AD tenant, and set the Access Level to Single Sign On. You will be prompted to sign-in to your Azure AD tenant. NOTE: You must sign-in with a user that is in the tenant; you cannot, during this step, sign-in with a Microsoft account.
127
+
2. In the `Models` folder add a new class called `TodoItem.cs`. Copy the implementation of TodoItem from this sample into the class.
128
+
3. Add a new, empty, Web API 2 controller called `TodoListController`.
129
+
4. Copy the implementation of the TodoListController from this sample into the controller. Don't forget to add the `[Authorize]` attribute to the class.
130
+
5. In `TodoListController` resolving missing references by adding `using` statements for `System.Collections.Concurrent`, `TodoListService.Models`, `System.Security.Claims`.
131
+
132
+
### Creating the TodoListClient Project
133
+
134
+
1. In the solution, create a new Windows --> WPF Application called TodoListClient.
135
+
2. Add the (stable) Active Directory Authentication Library (ADAL) NuGet, Microsoft.IdentityModel.Clients.ActiveDirectory, version 1.0.3 (or higher) to the project.
136
+
3. Add assembly references to `System.Net.Http` and `System.Web.Extensions`.
137
+
4. Add a new class to the project called `TodoItem.cs`. Copy the code from the sample project file of same name into this class, completely replacing the code in the file in the new project.
138
+
5. Add a new class to the project called `CacheHelper.cs`. Copy the code from the sample project file of same name into this class, completely replacing the code in the file in the new project.
139
+
6. Add a new class to the project called `CredManCache.cs`. Copy the code from the sample project file of same name into this class, completely replacing the code in the file in the new project.
140
+
7. Copy the markup from `MainWindow.xaml' in the sample project into the file of same name in the new project, completely replacing the markup in the file in the new project.
141
+
8. Copy the code from `MainWindow.xaml.cs` in the sample project into the file of same name in the new project, completely replacing the code in the file in the new project.
142
+
143
+
Finally, in the properties of the solution itself, set both projects as startup projects.
0 commit comments