Skip to content

Commit 3b3e312

Browse files
committed
Working sample and minimum README
1 parent 57ca56b commit 3b3e312

File tree

85 files changed

+28566
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+28566
-0
lines changed

.gitignore

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
2+
[Bb]in/
3+
[Oo]bj/
4+
5+
# mstest test results
6+
TestResults
7+
8+
## Ignore Visual Studio temporary files, build results, and
9+
## files generated by popular Visual Studio add-ons.
10+
11+
# User-specific files
12+
*.suo
13+
*.user
14+
*.sln.docstates
15+
16+
# Build results
17+
[Dd]ebug/
18+
[Rr]elease/
19+
x64/
20+
*_i.c
21+
*_p.c
22+
*.ilk
23+
*.meta
24+
*.obj
25+
*.pch
26+
*.pdb
27+
*.pgc
28+
*.pgd
29+
*.rsp
30+
*.sbr
31+
*.tlb
32+
*.tli
33+
*.tlh
34+
*.tmp
35+
*.log
36+
*.vspscc
37+
*.vssscc
38+
.builds
39+
40+
# Visual C++ cache files
41+
ipch/
42+
*.aps
43+
*.ncb
44+
*.opensdf
45+
*.sdf
46+
47+
# Visual Studio profiler
48+
*.psess
49+
*.vsp
50+
*.vspx
51+
52+
# Guidance Automation Toolkit
53+
*.gpState
54+
55+
# ReSharper is a .NET coding add-in
56+
_ReSharper*
57+
58+
# NCrunch
59+
*.ncrunch*
60+
.*crunch*.local.xml
61+
62+
# Installshield output folder
63+
[Ee]xpress
64+
65+
# DocProject is a documentation generator add-in
66+
DocProject/buildhelp/
67+
DocProject/Help/*.HxT
68+
DocProject/Help/*.HxC
69+
DocProject/Help/*.hhc
70+
DocProject/Help/*.hhk
71+
DocProject/Help/*.hhp
72+
DocProject/Help/Html2
73+
DocProject/Help/html
74+
75+
# Click-Once directory
76+
publish
77+
78+
# Publish Web Output
79+
*.Publish.xml
80+
81+
# NuGet Packages Directory
82+
packages
83+
84+
# Windows Azure Build Output
85+
csx
86+
*.build.csdef
87+
88+
# Windows Store app package directory
89+
AppPackages/
90+
91+
# Others
92+
[Bb]in
93+
[Oo]bj
94+
sql
95+
TestResults
96+
[Tt]est[Rr]esult*
97+
*.Cache
98+
ClientBin
99+
[Ss]tyle[Cc]op.*
100+
~$*
101+
*.dbmdl
102+
Generated_Code #added for RIA/Silverlight projects
103+
104+
# Backup & report files from converting an old project file to a newer
105+
# Visual Studio version. Backup files are not needed, because we have git ;-)
106+
_UpgradeReport_Files/
107+
Backup*/
108+
UpgradeLog*.XML

README.md

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,181 @@ WebAPI-ManuallyValidateJwt-DotNet
22
=================================
33

44
This sample demonstrates how to manually process a JWT access token using the JSON Web Token Handler For the Microsoft .Net Framework 4.5. This sample is equivalent to the NativeClient-DotNet sample, except instead of using OWIN middleware to process the token, the token is processed in application code.
5+
6+
For more information about how the protocols work in this scenario and other scenarios, see [Authentication Scenarios for Azure AD](http://go.microsoft.com/fwlink/?LinkId=394414).
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.
16+
17+
### Step 1: Clone or download this repository
18+
19+
From your shell or command line:
20+
21+
`git clone git@github.com:AzureADSamples/WebAPI-ManuallyValidateJwt-DotNet.git`
22+
23+
### 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-ManualJwt 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-ManualJwt", 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:44324/`.
41+
9. For the App ID URI, enter `https://<your_tenant_name>/TodoListService-ManualJwt`, 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",
55+
"impersonationAccessGrantTypes": [
56+
{
57+
"impersonated": "User",
58+
"impersonator": "Application"
59+
}
60+
],
61+
"isDisabled": false,
62+
"origin": "Application",
63+
"permissionId": "b69ee3c9-c40d-4f2a-ac80-961cd1534e40",
64+
"resourceScopeType": "Personal",
65+
"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-ManualJwt, 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-ManualJwt 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-ManualJwt`.
93+
94+
#### Configure the TodoListClient project
95+
96+
1. Open `app.config`.
97+
2. Find the app key `ida:Tenant` and replace the value with your AAD tenant name.
98+
3. Find the app key `ida:ClientId` and replace the value with the Client ID for the TodoListClient from the Azure portal.
99+
4. Find the app key `ida:RedirectUri` and replace the value with the Redirect URI for the TodoListClient from the Azure portal, for example `http://TodoListClient`.
100+
5. Find the app key `todo:TodoListResourceId` and replace the value with the App ID URI of the TodoListService-ManualJwt project, for example `https://<your_tenant_name>/TodoListService-ManualJwt`
101+
6. Find the app key `todo:TodoListBaseAddress` and replace the value with the base address of the TodoListService-ManualJwt project, for example `https://localhost:44324`.
102+
103+
### Step 5: Trust the IIS Express SSL certificate
104+
105+
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. Use the following steps in Windows Powershell to trust the IIS Express SSL certificate. You only need to do this once. If you fail to do this step, calls to the TodoListService-ManualJwt web API will always throw an unhandled exception where the inner exception message is:
106+
107+
"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."
108+
109+
To configure your computer to trust the IIS Express SSL certificate, begin by opening a Windows Powershell command window as Administrator.
110+
111+
Query your personal certificate store to find the thumbprint of the certificate for `CN=localhost`:
112+
113+
```
114+
PS C:\windows\system32> dir Cert:\LocalMachine\My
115+
116+
117+
Directory: Microsoft.PowerShell.Security\Certificate::LocalMachine\My
118+
119+
120+
Thumbprint Subject
121+
---------- -------
122+
C24798908DA71693C1053F42A462327543B38042 CN=localhost
123+
```
124+
125+
Next, add the certificate to the Trusted Root store:
126+
127+
```
128+
PS C:\windows\system32> $cert = (get-item cert:\LocalMachine\My\C24798908DA71693C1053F42A462327543B38042)
129+
PS C:\windows\system32> $store = (get-item cert:\Localmachine\Root)
130+
PS C:\windows\system32> $store.Open("ReadWrite")
131+
PS C:\windows\system32> $store.Add($cert)
132+
PS C:\windows\system32> $store.Close()
133+
```
134+
135+
You can verify the certificate is in the Trusted Root store by running this command:
136+
137+
`PS C:\windows\system32> dir Cert:\LocalMachine\Root`
138+
139+
### Step 6: Run the sample
140+
141+
Clean the solution, rebuild the solution, and run it. You might want to go into the solution properties and set both projects as startup projects, with the service project starting first.
142+
143+
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.
144+
145+
## How To Deploy This Sample to Azure
146+
147+
Coming soon.
148+
149+
## About The Code
150+
151+
Coming soon.
152+
153+
## How To Recreate This Sample
154+
155+
First, in Visual Studio 2013 create an empty solution to host the projects. Then, follow these steps to create each project.
156+
157+
### Creating the TodoListService-ManualJwt Project
158+
159+
1. In the solution, create a new ASP.Net MVC web API project called TodoListService-ManualJwt and while creating the project, ensure Authentication is set to No Authentication.
160+
2. Set SSL Enabled to be True. Note the SSL URL.
161+
3. In the project properties, Web properties, set the Project Url to be the SSL URL.
162+
4. Add the (stable) JSON Web Token Handler For the Microsoft .Net Framework 4.5 NuGet, System.IdentityModel.Tokens.Jwt, version 3.0.2 (or higher) to the project.
163+
5. Add an assembly reference to `System.IdentityModel`.
164+
6. In the `Models` folder add a new class called `TodoItem.cs`. Copy the implementation of TodoItem from this sample into the class.
165+
7. Add a new, empty, Web API 2 controller called `TodoListController`.
166+
8. Copy the implementation of the TodoListController from this sample into the controller.
167+
9. Open Global.asax, and copy the implementation from this sample into the controller. Note that a single line is added at the end of Application_Start(), `GlobalConfiguration.Configuration.MessageHandlers.Add(new TokenValidationHandler());`.
168+
10. In `web.config` create keys for `ida:AADInstance`, `ida:Tenant`, and `ida:Audience` and set them accordingly. For the public Azure cloud, the value of `ida:AADInstance` is `https://login.windows.net/{0}`.
169+
170+
### Creating the TodoListClient Project
171+
172+
1. In the solution, create a new Windows --> WPF Application called TodoListClient.
173+
2. Add the (stable) Active Directory Authentication Library (ADAL) NuGet, Microsoft.IdentityModel.Clients.ActiveDirectory, version 1.0.3 (or higher) to the project.
174+
3. Add assembly references to `System.Net.Http`, `System.Web.Extensions`, and `System.Configuration`.
175+
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.
176+
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.
177+
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.
178+
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.
179+
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.
180+
9. In `app.config` create keys for `ida:AADInstance`, `ida:Tenant`, `ida:ClientId`, `ida:RedirectUri`, `todo:TodoListResourceId`, and `todo:TodoListBaseAddress` and set them accordingly. For the public Azure cloud, the value of `ida:AADInstance` is `https://login.windows.net/{0}`.
181+
182+
Finally, in the properties of the solution itself, set both projects as startup projects.

TodoListClient/App.config

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
<appSettings>
7+
<add key="ida:AADInstance" value="https://login.windows.net/{0}" />
8+
<add key="ida:Tenant" value="[Enter tenant name, e.g. contoso.onmicrosoft.com]" />
9+
<add key="ida:ClientId" value="[Enter client ID as obtained from Azure Portal, e.g. 82692da5-a86f-44c9-9d53-2f88d52b478b]" />
10+
<add key="ida:RedirectUri" value="[Enter redirect URI as entered in Azure Portal, e.g. http://TodoListClient]" />
11+
<add key="todo:TodoListResourceId" value="[Enter App ID URI of TodoListService, e.g. https://contoso.onmicrosoft.com/TodoListService-ManualJwt]" />
12+
<add key="todo:TodoListBaseAddress" value="https://localhost:44324" />
13+
</appSettings>
14+
</configuration>

TodoListClient/App.xaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Application x:Class="TodoListClient.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
StartupUri="MainWindow.xaml">
5+
<Application.Resources>
6+
7+
</Application.Resources>
8+
</Application>

TodoListClient/App.xaml.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace TodoListClient
10+
{
11+
/// <summary>
12+
/// Interaction logic for App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
}
17+
}

0 commit comments

Comments
 (0)