This page describes how to run the Tailspin Surveys application locally, from Visual Studio. In these steps, you won't deploy the application to Azure. However, you will need to create some Azure resources — an Azure Active Directory (Azure AD) directory and a Redis cache.
Here is a summary of the steps:
- Create an Azure AD directory (tenant) for the fictitious Tailspin company.
- Register the Surveys application and the backend web API with Azure AD.
- Create an Azure Redis Cache instance.
- Configure application settings and create a local database.
- Run the application and sign up a new tenant.
- Add application roles to users.
- Visual Studio 2017 with the ASP.NET and web development workload installed
- Microsoft Azure account
Tailspin is the fictitious company that hosts the Surveys application. Tailspin uses Azure AD to enable other tenants to register with the app. Those customers can then use their Azure AD credentials to sign into the app.
In this step, you'll create an Azure AD directory for Tailspin.
-
Sign in to the Azure portal.
-
In the left pane, select Create a resource.
-
Select the Identity category, and then select the Azure Active Directory resource type.
-
Enter
Tailspin
for the organization name, and a domain name. The domain name will have the formxxxx.onmicrosoft.com
and must be globally unique. -
Select the Create button to create the new Tenant.
To complete the end-to-end scenario, you'll need a second Azure AD directory to represent a customer that signs up for the application. You can use your default Azure AD directory (not Tailspin), or create a new directory for this purpose. In the examples, we use Contoso as the fictitious customer.
-
In the Azure portal, switch to the newly created Tailspin directory by selecting your account in the top right corner of the portal and choosing Switch Directory, then select Tailspin from the directory list.
-
In the left pane, choose Azure Active Directory, then select App Registrations
-
Select New registration.
-
Enter the following information:
- Name:
Surveys.WebAPI
- Supported Account Types:
Accounts in any organizational directory (Any Azure AD directory - Multitenant)
- Application type:
Web
- Redirect URI:
https://localhost:44301/
- Name:
-
Select Register.
-
In App Registrations , select the Surveys.WebAPI application.
-
Select Expose an API > Application ID URI Set.
-
In the Set the App ID URI edit box, enter
https://<domain>/surveys.webapi
, where<domain>
is the domain name of the directory. For example:https://tailspin.onmicrosoft.com/surveys.webapi
-
Select Save.
-
Select Add a scope to define an API access scope that will later be used to authorize clients. Enter the following information:
-
Click Add scope
-
In App Registrations, select New Registration.
-
Enter the following information:
- Name:
Surveys
- Supported Account Types:
Accounts in any organizational directory (Any Azure AD directory - Multitenant)
- Application type:
Web
- Redirect URI:
https://localhost:44300/
Notice that the sign-on URL has a different port number from the
Surveys.WebAPI
app in the previous step. - Name:
-
Select Register.
-
In App Registrations, select the new Surveys application. Copy the application ID. You will need this later.
-
Select Expose an API > Application ID URI Set
-
In Set the App ID URI, enter
https://<domain>/surveys
, where<domain>
is the domain name of the directory. -
Select Save.
-
Select Authentication
-
Under Redirect URIs, select Add URI and enter:
https://localhost:44300/signin-oidc
. -
Under Implicit grant, select ID Tokens
-
Select Save.
-
Select Certificates and Secrets
-
Under Client secrets, select New client secret.
-
Enter a description, such as
client secret
. -
Under Expires, select In 1 year.
-
Select Add. The client secret will be generated at this point.
-
Before you navigate away from this blade, copy the value of the secret.
The value of the secret will not be visible again after you navigate away from the blade.
-
Select API Permissions, then select Add a permission.
-
In Request API Permissions select My APIs.
-
Select
Surveys.WebAPI
. -
Under What type of permissions does your application require?, select Delegated Permissions.
-
Under Select Permissions, select the survey.access scope.
-
Select Add permissions
-
In App Registrations , select the Surveys.WebAPI application.
-
Select Manifest.
-
Add the following JSON to the
appRoles
element. Generate new GUIDs for theid
properties.{ "allowedMemberTypes": ["User"], "description": "Creators can create surveys", "displayName": "SurveyCreator", "id": "<Generate a new GUID. Example: 1b4f816e-5eaf-48b9-8613-7923830595ad>", "isEnabled": true, "value": "SurveyCreator" }, { "allowedMemberTypes": ["User"], "description": "Administrators can manage the surveys in their tenant", "displayName": "SurveyAdmin", "id": "<Generate a new GUID>", "isEnabled": true, "value": "SurveyAdmin" }
-
In the
knownClientApplications
property, add the application ID for the Surveys web application, which you got when you registered the Surveys application earlier. For example:"knownClientApplications": ["be2cea23-aa0e-4e98-8b21-2963d494912e"],
This setting adds the Surveys app to the list of clients authorized to call the web API.
-
Select Save.
Now repeat the same steps for the Surveys app, except do not add an entry for knownClientApplications
. Use the same role definitions, but generate new GUIDs for the IDs.
The Surveys application uses Redis to cache OAuth 2 access tokens. To create the cache:
-
In the Azure portal
-
In the left pane, select Create a resource.
-
Select Databases > Azure Cache for Redis.
-
Enter the required information, including DNS name, resource group, location, and pricing tier. You can create a new resource group or use an existing resource group.
-
Select Create.
-
After the Redis cache is created, navigate to the resource in the portal.
-
Click Access keys and copy the primary key.
For more information about creating a Redis cache, see Quickstart: Use Azure Cache for Redis with a .NET Framework application.
-
Open the Tailspin.Surveys solution in Visual Studio.
-
In Solution Explorer, right-click the Tailspin.Surveys.Web project and select Manage User Secrets.
-
In the secrets.json file, paste in the following:
{ "AzureAd": { "ClientId": "<Surveys application ID>", "ClientSecret": "<Surveys app client secret>", "PostLogoutRedirectUri": "https://localhost:44300/", "WebApiResourceId": "<Surveys.WebAPI app ID URI>" }, "Redis": { "Configuration": "<Redis DNS name>.redis.cache.windows.net,password=<Redis primary key>,ssl=true" } }
Replace the items shown in angle brackets, as follows:
AzureAd:ClientId
: The application ID of the Surveys app.AzureAd:ClientSecret
: The key that you generated when you registered the Surveys application in Azure AD.AzureAd:WebApiResourceId
: The App ID URI that you specified when you created the Surveys.WebAPI application in Azure AD. It should have the formhttps://<directory>.onmicrosoft.com/surveys.webapi
Redis:Configuration
: Build this string from the DNS name of the Redis cache and the primary access key. For example, "tailspin.redis.cache.windows.net,password=2h5tBxxx,ssl=true".
-
Save the updated secrets.json file.
-
Repeat these steps for the Tailspin.Surveys.WebAPI project, but paste the following into secrets.json. Replace the items in angle brackets, as before.
{ "AzureAd": { "WebApiResourceId": "<Surveys.WebAPI app ID URI>" }, "Redis": { "Configuration": "<Redis DNS name>.redis.cache.windows.net,password=<Redis primary key>,ssl=true" } }
In this step, you will use Entity Framework 7 to create a local SQL database, using LocalDB.
-
Open a command window
-
Navigate to the Tailspin.Surveys.Data project.
-
Run the following command:
dotnet ef database update --startup-project ..\Tailspin.Surveys.Web
To run the application, start both the Tailspin.Surveys.Web and Tailspin.Surveys.WebAPI projects.
You can set Visual Studio to run both projects automatically on F5, as follows:
- In Solution Explorer, right-click the solution and click Set Startup Projects.
- Select Multiple startup projects.
- Set Action = Start for the Tailspin.Surveys.Web and Tailspin.Surveys.WebAPI projects.
When the application starts, you are not signed in, so you see the welcome page:
To sign up an organization:
- Click Enroll your company in Tailspin.
- Sign in to the Azure AD directory that represents the organization using the Surveys app. You must sign in as an admin user.
- Accept the consent prompt.
The application registers the tenant, and then signs you out. The app signs you out because you need to set up the application roles in Azure AD, before using the application.
When a tenant signs up, an AD admin for the tenant must assign application roles to users.
-
In the Azure portal, switch to the Azure AD directory that you used to sign up for the Surveys app.
-
In the left pane, choose Azure Active Directory, then select Enterprise Applications. You should see the applications
Survey
andSurvey.WebAPI
listed. If not, make sure that you completed the sign up process. -
Select the Surveys application.
-
Select Users and Groups > Add user.
-
If you have Azure AD Premium, click Users and groups. Otherwise, click Users. (Assigning a role to a group requires Azure AD Premium.)
-
Select one or more users and click Select.
-
Select the role and click Select.
-
Click Assign.
Repeat the same steps to assign roles for the Survey.WebAPI application.
A user should always have the same roles in both Survey and Survey.WebAPI. Otherwise, the user will have inconsistent permissions, which may lead to 403 (Forbidden) errors from the Web API.
Now go back to the app and sign in again. Click My Surveys. If the user is assigned to the SurveyAdmin or SurveyCreator role, you will see a Create Survey button, indicating that the user has permissions to create a new survey.
As a security best practice, you should never store application secrets such as connection strings in source control. To enable storing secrets in Key Vault, follow the steps here.