Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit d9a24c3

Browse files
committed
Pre-release update
-cosmetic edits - Added latest ADAL RC - Added readme - generalized app settings
1 parent baca839 commit d9a24c3

6 files changed

Lines changed: 189 additions & 20 deletions

File tree

README.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
NativeClient-Headless-DotNet
2+
===================
3+
4+
This sample demonstrates a .Net console 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. This sample shows you how to use ADAL to authenticate users via raw credentials (username and password, or Windows integrated authentication) via a text-only interface.
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/NativeClient-Headless-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 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 Manifest 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-Headless-DotNet", select "Native Client Application", and click next.
80+
8. For the Redirect URI, enter `http://TodoListClient`. Please note that the Redirect URI will not be used in this sample, but it needs to be defined nonetheless. 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+
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 `todo:TodoListResourceId` and replace the value with the App ID URI of the TodoListService, for example `https://<your_tenant_name>/TodoListService`
100+
5. Find the app key `todo:TodoListBaseAddress` and replace the value with the base address of the 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. 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 will always throw an unhandled exception where the inner exception message is:
105+
106+
"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."
107+
108+
To configure your computer to trust the IIS Express SSL certificate, begin by opening a Windows Powershell command window as Administrator.
109+
110+
Query your personal certificate store to find the thumbprint of the certificate for `CN=localhost`:
111+
112+
```
113+
PS C:\windows\system32> dir Cert:\LocalMachine\My
114+
115+
116+
Directory: Microsoft.PowerShell.Security\Certificate::LocalMachine\My
117+
118+
119+
Thumbprint Subject
120+
---------- -------
121+
C24798908DA71693C1053F42A462327543B38042 CN=localhost
122+
```
123+
124+
Next, add the certificate to the Trusted Root store:
125+
126+
```
127+
PS C:\windows\system32> $cert = (get-item cert:\LocalMachine\My\C24798908DA71693C1053F42A462327543B38042)
128+
PS C:\windows\system32> $store = (get-item cert:\Localmachine\Root)
129+
PS C:\windows\system32> $store.Open("ReadWrite")
130+
PS C:\windows\system32> $store.Add($cert)
131+
PS C:\windows\system32> $store.Close()
132+
```
133+
134+
You can verify the certificate is in the Trusted Root store by running this command:
135+
136+
`PS C:\windows\system32> dir Cert:\LocalMachine\Root`
137+
138+
### Step 6: Run the sample
139+
140+
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.
141+
142+
Type the command `add`. You will be prompted for your username and password. Enter them, then enter the description a new todo. That done, type the command `list`. You will see that the command executes without prompting you again. You can clear the token cache by typing `clear`, and leave the client by typing `exit`.
143+
Notice that if you stop the application without clearing the cache, 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+
To deploy the TodoListService to Azure Web Sites, you will create a web site, publish the TodoListService to the web site, and update the TodoListClient to call the web site instead of IIS Express.
148+
149+
### Create and Publish the TodoListService to an Azure Web Site
150+
151+
1. Sign in to the [Azure management portal](https://manage.windowsazure.com).
152+
2. Click on Web Sites in the left hand nav.
153+
3. Click New in the bottom left hand corner, select Compute --> Web Site --> Quick Create, select the hosting plan and region, and give your web site a name, e.g. todolistservice-contoso.azurewebsites.net. Click Create Web Site.
154+
4. Once the web site is created, click on it to manage it. For this set of steps, download the publish profile and save it. Other deployment mechanisms, such as from source control, can also be used.
155+
5. Switch to Visual Studio and go to the TodoListService project. Right click on the project in the Solution Explorer and select Publish. Click Import, and import the publish profile that you just downloaded.
156+
6. On the Connection tab, update the Destination URL so that it is https, for example https://todolistservice-skwantoso.azurewebsites.net. Click Next.
157+
7. On the Settings tab, make sure Enable Organizational Authentication is NOT selected. Click Publish.
158+
8. Visual Studio will publish the project and automatically open a browser to the URL of the project. If you see the default web page of the project, the publication was successful.
159+
160+
### Update the TodoListClient to call the TodoListService Running in Azure Web Sites
161+
162+
1. In Visual Studio, go to the TodoListClient project.
163+
2. Open `app.config`. Only one change is needed - update the `todo:TodoListBaseAddress` key value to be the address of the website you published, e.g. https://todolistservice-skwantoso.azurewebsites.net.
164+
3. Run the client! If you are trying multiple different client types (e.g. .Net, Windows Store, Android, iOS) you can have them all call this one published web API.
165+
166+
NOTE: Remember, the To Do list is stored in memory in this TodoListService sample. Azure Web Sites will spin down your web site if it is inactive, and your To Do list will get emptied. Also, if you increase the instance count of the web site, requests will be distributed among the instances and the To Do will not be the same on each instance.
167+
168+
## About The Code
169+
170+
Coming soon.
171+
172+
## How To Recreate This Sample
173+
174+
coming soon.

TodoListClient/App.config

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@
44
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
55
</startup>
66
<appSettings>
7-
<add key="ida:AADInstance" value="https://login.windows.net/{0}" />
8-
<add key="ida:Tenant" value="developertenant.onmicrosoft.com" />
9-
<add key="ida:ClientId" value="83a417dd-2860-41ec-a4d0-178d4064d5f0" />
10-
<add key="ida:RedirectUri" value="http://BasicTodoListClient" />
11-
<add key="todo:TodoListResourceId" value="https://developertenant.onmicrosoft.com/BasicTodoService" />
12-
<add key="todo:TodoListBaseAddress" value="https://localhost:44321" />
13-
<!-- <add key="ida:AADInstance" value="https://login.windows.net/{0}" />
14-
<add key="ida:Tenant" value="[Enter tenant name, e.g. contoso.onmicrosoft.com]" />
15-
<add key="ida:ClientId" value="[Enter client ID as obtained from Azure Portal, e.g. 82692da5-a86f-44c9-9d53-2f88d52b478b]" />
16-
<add key="ida:RedirectUri" value="[Enter redirect URI as entered in Azure Portal, e.g. http://TodoListClient]" />
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]" />
1710
<add key="todo:TodoListResourceId" value="[Enter App ID URI of TodoListService, e.g. https://contoso.onmicrosoft.com/TodoListService]" />
18-
<add key="todo:TodoListBaseAddress" value="https://localhost:44321" />-->
11+
<add key="todo:TodoListBaseAddress" value="https://localhost:44321" />
1912
</appSettings>
2013
</configuration>

TodoListClient/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ static void ShowError(Exception ex)
9797
string message = ex.Message;
9898
if (ex.InnerException != null)
9999
{
100-
message += "Inner Exception : " + ex.InnerException.Message;
100+
message += Environment.NewLine + "Inner Exception : " + ex.InnerException.Message;
101101
}
102-
Console.WriteLine("Message");
102+
Console.WriteLine("Message: {0}",message);
103103
}
104104

105105
// Obscure the password being entered

TodoListClient/TodoListClient.csproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35-
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory">
36-
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.7.10703.1814-rc\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
35+
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
36+
<SpecificVersion>False</SpecificVersion>
37+
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.7.10707.1513-rc\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
3738
</Reference>
38-
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms">
39-
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.7.10703.1814-rc\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll</HintPath>
39+
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms, Version=2.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
40+
<SpecificVersion>False</SpecificVersion>
41+
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.7.10707.1513-rc\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll</HintPath>
4042
</Reference>
4143
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
4244
<SpecificVersion>False</SpecificVersion>

TodoListClient/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<packages>
33
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
44
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
5-
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.7.10703.1814-rc" targetFramework="net45" />
5+
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.7.10707.1513-rc" targetFramework="net45" />
66
<package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net45" />
77
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net45" />
88
</packages>

TodoListService/Web.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<add key="webpages:Enabled" value="false" />
1010
<add key="ClientValidationEnabled" value="true" />
1111
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
12-
<add key="ida:Tenant" value="developertenant.onmicrosoft.com" />
13-
<add key="ida:Audience" value="https://developertenant.onmicrosoft.com/BasicTodoService" />
12+
<add key="ida:Tenant" value="[Enter tenant name, e.g. contoso.onmicrosoft.com]" />
13+
<add key="ida:Audience" value="[Enter App ID URI of TodoListService, e.g. https://contoso.onmicrosoft.com/TodoListService]" />
1414
</appSettings>
1515
<system.web>
1616
<compilation debug="true" targetFramework="4.5" />

0 commit comments

Comments
 (0)