Skip to content

Commit de4c2c4

Browse files
author
Tiago Brenck
committed
Merge branch 'master' into tibre/refactor
2 parents 2bb935d + 5104f3a commit de4c2c4

File tree

375 files changed

+80603
-20447
lines changed

Some content is hidden

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

375 files changed

+80603
-20447
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ IF SUFFICIENT INFORMATION IS NOT PROVIDED VIA THE FOLLOWING TEMPLATE THE ISSUE M
1616

1717
Please add an 'x' for the scenario(s) where you found an issue
1818

19-
1. [ ] Web app that signs in users
19+
1. Web app that signs in users
2020
1. [ ] with a work and school account in your organization: [1-WebApp-OIDC/1-1-MyOrg](../blob/master/1-WebApp-OIDC/1-1-MyOrg)
2121
1. [ ] with any work and school account: [/1-WebApp-OIDC/1-2-AnyOrg](../blob/master/1-WebApp-OIDC/1-2-AnyOrg)
2222
1. [ ] with any work or school account or Microsoft personal account: [1-WebApp-OIDC/1-3-AnyOrgOrPersonal](../blob/master/1-WebApp-OIDC/1-3-AnyOrgOrPersonal)
@@ -27,7 +27,7 @@ Please add an 'x' for the scenario(s) where you found an issue
2727
1. [ ] With specific token caches: [2-WebApp-graph-user/2-2-TokenCache](../blob/master/2-WebApp-graph-user/2-2-TokenCache)
2828
1. [ ] Calling Microsoft Graph in national clouds: [2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph](../blob/master/2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph)
2929
1. [ ] Web app calling several APIs [3-WebApp-multi-APIs](../blob/master/3-WebApp-multi-APIs)
30-
1. [ ] Web app calling your own Web API
30+
1. [ ] Web app calling your own Web API [4-WebApp-your-API](../blob/master/4-WebApp-your-API)
3131
1. Web app restricting users
3232
1. [ ] by Roles: [5-WebApp-AuthZ/5-1-Roles](../blob/master/5-WebApp-AuthZ/5-1-Roles)
3333
1. [ ] by Groups: [5-WebApp-AuthZ/5-2-Groups](../blob/master/5-WebApp-AuthZ/5-2-Groups)

.gitignore

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
################################################################################
1+
################################################################################
22
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
33
################################################################################
44

@@ -35,12 +35,27 @@
3535
/2-WebApp-graph-user/2-3-Best-Practices/.vs
3636
/2-WebApp-graph-user/2-3-Best-Practices/bin
3737
/2-WebApp-graph-user/2-3-Best-Practices/obj
38+
/2-WebApp-graph-user/2-3-Multi-Tenant/.vs
39+
/2-WebApp-graph-user/2-3-Multi-Tenant/bin
40+
/2-WebApp-graph-user/2-3-Multi-Tenant/obj
41+
/2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph/.vs
42+
/2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph/bin
43+
/2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph/obj
3844
/3-WebApp-multi-APIs/.vs
3945
/3-WebApp-multi-APIs/bin
4046
/3-WebApp-multi-APIs/obj
4147
/4-WebApp-your-API/.vs
4248
/4-WebApp-your-API/bin
4349
/4-WebApp-your-API/obj
50+
/4-WebApp-your-API/Client/.vs
51+
/4-WebApp-your-API/Client/bin
52+
/4-WebApp-your-API/Client/obj
53+
/5-WebApp-AuthZ/5-1-Roles/.vs
54+
/5-WebApp-AuthZ/5-1-Roles/bin
55+
/5-WebApp-AuthZ/5-1-Roles/obj
56+
/5-WebApp-AuthZ/5-2-Groups/.vs
57+
/5-WebApp-AuthZ/5-2-Groups/bin
58+
/5-WebApp-AuthZ/5-2-Groups/obj
4459
/5-WebApp-AuthZ-Roles-Groups/.vs
4560
/5-WebApp-AuthZ-Roles-Groups/bin
4661
/5-WebApp-AuthZ-Roles-Groups/obj
@@ -96,3 +111,14 @@
96111
/4-WebApp-your-API/TodoListService/obj
97112
/4-WebApp-your-API/TodoListService/bin
98113
/4-WebApp-your-API/Client/obj
114+
/2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph/bin/Debug/netcoreapp2.2
115+
/2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph/obj
116+
/2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph/bin/Release/netcoreapp2.2
117+
/Microsoft.Identity.Web.Test/bin/Release/netcoreapp2.2
118+
/Microsoft.Identity.Web.Test/obj
119+
/4-WebApp-your-API/4-2-B2C/.vs
120+
/4-WebApp-your-API/4-2-B2C/Client/obj
121+
/4-WebApp-your-API/4-2-B2C/TodoListService/obj
122+
/2-WebApp-graph-user/2-3-Multi-Tenant/.vs/WebApp-OpenIDConnect-DotNet
123+
/2-WebApp-graph-user/2-3-Multi-Tenant/bin/Debug/netcoreapp2.2
124+
/2-WebApp-graph-user/2-3-Multi-Tenant/obj
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Authorization;
22
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Extensions.Logging;
34
using System.Diagnostics;
45
using WebApp_OpenIDConnect_DotNet.Models;
56

@@ -8,20 +9,28 @@ namespace WebApp_OpenIDConnect_DotNet.Controllers
89
[Authorize]
910
public class HomeController : Controller
1011
{
11-
public HomeController()
12+
private readonly ILogger<HomeController> _logger;
13+
14+
public HomeController(ILogger<HomeController> logger)
1215
{
16+
_logger = logger;
1317
}
1418

1519
public IActionResult Index()
1620
{
1721
return View();
1822
}
1923

24+
public IActionResult Privacy()
25+
{
26+
return View();
27+
}
28+
2029
[AllowAnonymous]
2130
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
2231
public IActionResult Error()
2332
{
24-
return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
33+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
2534
}
2635
}
2736
}

1-WebApp-OIDC/1-1-MyOrg/Program.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
using Microsoft.AspNetCore;
2-
using Microsoft.AspNetCore.Hosting;
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
33

44
namespace WebApp_OpenIDConnect_DotNet
55
{
66
public class Program
77
{
88
public static void Main(string[] args)
99
{
10-
CreateWebHostBuilder(args).Build().Run();
10+
CreateHostBuilder(args).Build().Run();
1111
}
1212

13-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
14-
WebHost.CreateDefaultBuilder(args)
15-
.UseStartup<Startup>();
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureWebHostDefaults(webBuilder =>
16+
{
17+
webBuilder.UseStartup<Startup>();
18+
});
1619
}
1720
}

1-WebApp-OIDC/1-1-MyOrg/Properties/launchSettings.json

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,13 @@
88
}
99
},
1010
"profiles": {
11-
"IIS Express": {
12-
"commandName": "IISExpress",
13-
"launchBrowser": true,
14-
"environmentVariables": {
15-
"ASPNETCORE_ENVIRONMENT": "Development"
16-
}
17-
},
18-
"webApp": {
11+
"WebApp_OpenIDConnect_DotNet": {
1912
"commandName": "Project",
2013
"launchBrowser": true,
14+
"applicationUrl": "https://localhost:44366;http://localhost:3110",
2115
"environmentVariables": {
2216
"ASPNETCORE_ENVIRONMENT": "Development"
23-
},
24-
"applicationUrl": "http://localhost:3110/"
17+
}
2518
}
2619
}
2720
}

1-WebApp-OIDC/1-1-MyOrg/README.md

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ platforms: dotnet
44
author: jmprieur
55
level: 100
66
client: ASP.NET Core Web App
7-
endpoint: AAD v2.0
7+
endpoint: Microsoft identity platform
88
---
99

1010
# An ASP.NET Core Web app signing-in users with the Microsoft identity platform in your organization
1111

12-
> This sample is for Azure AD, not Azure AD B2C. See [active-directory-b2c-dotnetcore-webapp](https://github.com/Azure-Samples/active-directory-b2c-dotnetcore-webapp), until we incorporate the B2C variation in the tutorial.
12+
> This sample is for Azure AD, not Azure AD B2C. See [sample 1-5-B2C](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/1-WebApp-OIDC/1-5-B2C), for B2C scenario.
1313
1414
[![Build status](https://identitydivision.visualstudio.com/IDDP/_apis/build/status/AAD%20Samples/.NET%20client%20samples/ASP.NET%20Core%20Web%20App%20tutorial)](https://identitydivision.visualstudio.com/IDDP/_build/latest?definitionId=819)
1515

1616
## Scenario
1717

18-
This sample shows how to build a .NET Core 2.2 MVC Web app that uses OpenID Connect to sign in users. Users can only sign-in with their `work and school` accounts in their own organization. It leverages the ASP.NET Core OpenID Connect middleware.
18+
This sample shows how to build a .NET Core MVC Web app that uses OpenID Connect to sign in users. Users can only sign-in with their `work and school` accounts in their own organization. It leverages the ASP.NET Core OpenID Connect middleware.
1919

2020
![Sign in with Azure AD](ReadmeFiles/sign-in.png)
2121

@@ -26,32 +26,38 @@ This sample shows how to build a .NET Core 2.2 MVC Web app that uses OpenID Conn
2626

2727
To run this sample:
2828

29-
> Pre-requisites: Install .NET Core 2.2 or later (for example for Windows) by following the instructions at [.NET and C# - Get Started in 10 Minutes](https://www.microsoft.com/net/core). In addition to developing on Windows, you can develop on [Linux](https://www.microsoft.com/net/core#linuxredhat), [Mac](https://www.microsoft.com/net/core#macos), or [Docker](https://www.microsoft.com/net/core#dockercmd).
29+
> Pre-requisites: Install .NET Core 3.0 or later (for example for Windows) by following the instructions at [.NET and C# - Get Started in 10 Minutes](https://www.microsoft.com/net/core). In addition to developing on Windows, you can develop on [Linux](https://www.microsoft.com/net/core#linuxredhat), [Mac](https://www.microsoft.com/net/core#macos), or [Docker](https://www.microsoft.com/net/core#dockercmd).
3030
3131
### Step 1: Register the sample with your Azure AD tenant
3232

3333
There is one project in this sample. To register it, you can:
3434

35-
- either use PowerShell scripts that **automatically** creates the Azure AD applications and related objects (passwords, permissions, dependencies) for you and modify the Visual Studio projects' configuration files. If you want to use this automation:
35+
- either use PowerShell scripts that **automatically** creates the Azure AD applications and related objects (passwords, permissions, dependencies) for you and modify the Visual Studio projects' configuration files.
3636

37-
1. On Windows run PowerShell and navigate to the solution's folder
38-
2. In PowerShell run:
37+
<details>
38+
<summary>Expand to see how to use this automation</summary>
3939

40-
```PowerShell
41-
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
42-
```
40+
1. On Windows run PowerShell and navigate to the solution's folder
4341

44-
3. Run the script to create your Azure AD application and configure the code of the sample application accordinly
42+
2. In PowerShell run:
4543

46-
```PowerShell
47-
.\AppCreationScripts\Configure.ps1
48-
```
44+
```PowerShell
45+
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
46+
```
47+
48+
3. Run the script to create your Azure AD application and configure the code of the sample application accordingly
49+
50+
```PowerShell
51+
.\AppCreationScripts\Configure.ps1
52+
```
4953
50-
> Other ways of running the scripts are described in [App Creation Scripts](./AppCreationScripts/AppCreationScripts.md)
54+
> Other ways of running the scripts are described in [App Creation Scripts](./AppCreationScripts/AppCreationScripts.md)
5155
52-
4. Open the Visual Studio solution and click start. That's it!
56+
4. Open the Visual Studio solution and click start. That's it!
5357
54-
- or, if you don't want to use automation, follow the steps below:
58+
</details>
59+
60+
- or, if you want to register your application with the Azure portal, follow the steps below:
5561
5662
#### Choose the Azure AD tenant where you want to create your applications
5763
@@ -68,9 +74,23 @@ As a first step you'll need to:
6874
1. When the **Register an application page** appears, enter your application's registration information:
6975
- In the **Name** section, enter a meaningful application name that will be displayed to users of the app, for example `WebApp`.
7076
- In the **Supported account types** section, select **Accounts in this organizational directory only ({tenant name})**.
77+
<details open=true>
78+
<summary>Expand/collapse screenshot</summary>
79+
80+
![Register app](../../ReadmeFiles/screenshot-register-app.png)
81+
82+
</details>
7183
> Note that there are more than one redirect URIs. You'll need to add them from the **Authentication** tab later after the app has been created succesfully.
84+
7285
1. Select **Register** to create the application.
7386
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.
87+
<details open=true>
88+
<summary>Expand/collapse screenshot</summary>
89+
90+
![OVerview page](../../ReadmeFiles/screenshot-overview.png)
91+
92+
</details>
93+
7494
1. In the list of pages for the app, select **Authentication**..
7595
- In the Redirect URIs section, select **Web** in the combo-box and enter the following redirect URIs.
7696
- `https://localhost:44321/`
@@ -79,13 +99,20 @@ As a first step you'll need to:
7999
- In the **Advanced settings** | **Implicit grant** section, check **ID tokens** as this sample requires
80100
the [Implicit grant flow](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-implicit-grant-flow) to be enabled to
81101
sign-in the user.
102+
<details open=true>
103+
<summary>Expand/collapse screenshot</summary>
104+
105+
![Authentication page](../../ReadmeFiles/screenshot-authentication.png)
106+
107+
</details>
108+
82109
1. Select **Save**.
83110
84111
> Note that unless the Web App calls a Web API, no certificate or secret is needed.
85112
86113
### Step 2: Download/ Clone this sample code or build the application using a template
87114
88-
This sample was created from the dotnet core 2.2 [dotnet new mvc](https://docs.microsoft.com/dotnet/core/tools/dotnet-new?tabs=netcore2x) template with `SingleOrg` authentication, and then tweaked to let it support tokens for the Azure AD V2 endpoint. You can clone/download this repository or create the sample from the command line:
115+
This sample was created from the dotnet core 2.2 [dotnet new mvc](https://docs.microsoft.com/dotnet/core/tools/dotnet-new?tabs=netcore2x) template with `SingleOrg` authentication, and then tweaked to let it support tokens for the Microsoft identity platform endpoint. You can clone/download this repository or create the sample from the command line:
89116
90117
#### Option 1: Download/ clone this sample
91118
@@ -136,10 +163,10 @@ cd "1-WebApp-OIDC\1-1-MyOrg"
136163
by this line:
137164
138165
```CSharp
139-
services.AddAzureAdV2Authentication(Configuration);
166+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration);
140167
```
141168
142-
This enables your application to use the Microsoft identity platform (formerly Azure AD v2.0) endpoint. This endpoint is capable of signing-in users both with their Work and School and Microsoft Personal accounts.
169+
This enables your application to use the Microsoft identity platform endpoint. This endpoint is capable of signing-in users both with their Work and School and Microsoft Personal accounts.
143170
144171
1. Change the `Properties\launchSettings.json` file to ensure that you start your web app from <https://localhost:44321> as registered. For this:
145172
- update the `sslPort` of the `iisSettings` section to be `44321`
@@ -149,16 +176,15 @@ cd "1-WebApp-OIDC\1-1-MyOrg"
149176
150177
1. Build the solution and run it.
151178
152-
2. Open your web browser and make a request to the app. Accept the IIS Express SSL certificate if needed. The app immediately attempts to authenticate you via the Azure AD v2 endpoint. Sign in with your personal account or with work or school account.
153-
179+
2. Open your web browser and make a request to the app. Accept the IIS Express SSL certificate if needed. The app immediately attempts to authenticate you via the identity platform endpoint. Sign in with your personal account or with work or school account.
154180
155181
## Toubleshooting
156182
157183
### known issue on iOS 12
158184
159185
ASP.NET core applications create session cookies that represent the identity of the caller. Some Safari users using iOS 12 had issues which are described in [ASP.NET Core #4467](https://github.com/aspnet/AspNetCore/issues/4647) and the Web kit bugs database [Bug 188165 - iOS 12 Safari breaks ASP.NET Core 2.1 OIDC authentication](https://bugs.webkit.org/show_bug.cgi?id=188165).
160186
161-
If your web site needs to be accessed from users using iOS 12, you probably want to disable the SameSite protection, but also ensure that state changes are protected with CSRF anti-forgery mecanism. See the how to fix section of [Microsoft Security Advisory: iOS12 breaks social, WSFed and OIDC logins #4647](https://github.com/aspnet/AspNetCore/issues/4647)
187+
If your web site needs to be accessed from users using iOS 12, you probably want to disable the SameSite protection, but also ensure that state changes are protected with CSRF anti-forgery mechanism. See the how to fix section of [Microsoft Security Advisory: iOS12 breaks social, WSFed and OIDC logins #4647](https://github.com/aspnet/AspNetCore/issues/4647)
162188
163189
> Did the sample not work for you as expected? Did you encounter issues trying this sample? Then please reach out to us using the [GitHub Issues](../../../../issues) page.
164190

1-WebApp-OIDC/1-1-MyOrg/Startup.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using Microsoft.AspNetCore.Authentication.AzureAD.UI;
2-
using Microsoft.AspNetCore.Authorization;
1+
using Microsoft.AspNetCore.Authorization;
32
using Microsoft.AspNetCore.Builder;
43
using Microsoft.AspNetCore.Hosting;
54
using Microsoft.AspNetCore.Http;
6-
using Microsoft.AspNetCore.Mvc;
75
using Microsoft.AspNetCore.Mvc.Authorization;
86
using Microsoft.Extensions.Configuration;
97
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Hosting;
109
using Microsoft.Identity.Web;
1110

1211
namespace WebApp_OpenIDConnect_DotNet
@@ -31,20 +30,20 @@ public void ConfigureServices(IServiceCollection services)
3130
});
3231

3332
// Sign-in users with the Microsoft identity platform
34-
services.AddAzureAdV2Authentication(Configuration);
33+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration);
3534

36-
services.AddMvc(options =>
35+
services.AddControllersWithViews(options =>
3736
{
3837
var policy = new AuthorizationPolicyBuilder()
3938
.RequireAuthenticatedUser()
4039
.Build();
4140
options.Filters.Add(new AuthorizeFilter(policy));
42-
})
43-
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
41+
});
42+
services.AddRazorPages();
4443
}
4544

4645
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
47-
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
46+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4847
{
4948
if (env.IsDevelopment())
5049
{
@@ -61,13 +60,17 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
6160
app.UseStaticFiles();
6261
app.UseCookiePolicy();
6362

63+
app.UseRouting();
64+
6465
app.UseAuthentication();
66+
app.UseAuthorization();
6567

66-
app.UseMvc(routes =>
68+
app.UseEndpoints(endpoints =>
6769
{
68-
routes.MapRoute(
70+
endpoints.MapControllerRoute(
6971
name: "default",
70-
template: "{controller=Home}/{action=Index}/{id?}");
72+
pattern: "{controller=Home}/{action=Index}/{id?}");
73+
endpoints.MapRazorPages();
7174
});
7275
}
7376
}

1-WebApp-OIDC/1-1-MyOrg/Views/Home/Index.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
ASP.NET Core web app signing-in users in your organization
77
</h1>
88
<p>
9-
This sample shows how to build a .NET Core 2.2 MVC Web app that uses OpenID Connect to sign in users in your organization. It leverages the ASP.NET Core OpenID Connect middleware.
9+
This sample shows how to build a .NET Core MVC Web app that uses OpenID Connect to sign in users in your organization. It leverages the ASP.NET Core OpenID Connect middleware.
1010
</p>
1111
<img src="https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/raw/master/1-WebApp-OIDC/1-1-MyOrg/ReadmeFiles/sign-in.png
1212
"/>

1-WebApp-OIDC/1-1-MyOrg/Views/Shared/Error.cshtml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
1919
</p>
2020
<p>
21-
<strong>Development environment should not be enabled in deployed applications</strong>, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>, and restarting the application.
21+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
22+
It can result in displaying sensitive information from exceptions to end users.
23+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
24+
and restarting the app.
2225
</p>

0 commit comments

Comments
 (0)