Skip to content

Commit 535087d

Browse files
author
Tiago Brenck
committed
Changed user-flow so the home page allows anonymous access.
1 parent dde2533 commit 535087d

File tree

14 files changed

+68
-57
lines changed

14 files changed

+68
-57
lines changed

2-WebApp-graph-user/2-3-Multi-Tenant/Controllers/HomeController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
*/
2424

2525
using Microsoft.AspNetCore.Authorization;
26+
using Microsoft.AspNetCore.Http.Authentication;
2627
using Microsoft.AspNetCore.Mvc;
2728
using Microsoft.Identity.Web;
2829
using System.Diagnostics;
2930
using System.Linq;
31+
using System.Threading.Tasks;
3032
using WebApp_OpenIDConnect_DotNet.DAL;
3133
using WebApp_OpenIDConnect_DotNet.Models;
3234

3335
namespace WebApp_OpenIDConnect_DotNet.Controllers
3436
{
35-
[Authorize]
37+
[AllowAnonymous]
3638
public class HomeController : Controller
3739
{
3840
private readonly SampleDbContext dbContext;
@@ -62,14 +64,12 @@ public IActionResult DeleteTenant(string id)
6264
return RedirectToAction("Index");
6365
}
6466

65-
[AllowAnonymous]
6667
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
6768
public IActionResult UnauthorizedTenant()
6869
{
6970
return View();
7071
}
7172

72-
[AllowAnonymous]
7373
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
7474
public IActionResult Error()
7575
{

2-WebApp-graph-user/2-3-Multi-Tenant/Controllers/OnboardingController.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
*/
2424

25-
using System;
26-
using System.Collections.Generic;
27-
using System.Linq;
28-
using System.Threading.Tasks;
25+
using Microsoft.AspNetCore.Authentication;
2926
using Microsoft.AspNetCore.Authentication.AzureAD.UI;
3027
using Microsoft.AspNetCore.Authorization;
3128
using Microsoft.AspNetCore.Http.Extensions;
3229
using Microsoft.AspNetCore.Mvc;
3330
using Microsoft.Extensions.Options;
31+
using System;
32+
using System.Linq;
33+
using System.Threading.Tasks;
3434
using WebApp_OpenIDConnect_DotNet.DAL;
3535
using WebApp_OpenIDConnect_DotNet.Models;
3636

@@ -97,10 +97,14 @@ public async Task<IActionResult> ProcessCode(string tenant, string error, string
9797
return RedirectToAction("Error", "Home");
9898
}
9999

100+
var authenticationProperties = new AuthenticationProperties { RedirectUri = "Home/Index" };
101+
100102
// Check if tenant is already authorized
101-
if(dbContext.AuthorizedTenants.FirstOrDefault(x => x.TenantId == tenant) != null)
103+
if (dbContext.AuthorizedTenants.FirstOrDefault(x => x.TenantId == tenant) != null)
102104
{
103-
return RedirectToAction("Index", "Home");
105+
// Challenge an authentication so dotnet can set the user identity claims.
106+
// Since the user will have a session on AAD already, they wont need to select an account again.
107+
return Challenge(authenticationProperties, AzureADDefaults.OpenIdScheme);
104108
}
105109

106110
// Find a tenant carrying a TempAuthorizationCode that we previously saved
@@ -120,7 +124,9 @@ public async Task<IActionResult> ProcessCode(string tenant, string error, string
120124

121125
await dbContext.SaveChangesAsync();
122126

123-
return RedirectToAction("Index", "Home");
127+
// Challenge an authentication so dotnet can set the user identity claims.
128+
// Since the user will have a session on AAD already, they wont need to select an account again.
129+
return Challenge(authenticationProperties, AzureADDefaults.OpenIdScheme);
124130
}
125131
}
126132
}

2-WebApp-graph-user/2-3-Multi-Tenant/Controllers/TodoListController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3030
using Microsoft.AspNetCore.Mvc;
3131
using Microsoft.AspNetCore.Mvc.Rendering;
3232
using Microsoft.Identity.Web;
33-
using WebApp_OpenIDConnect_DotNet.BLL;
33+
using WebApp_OpenIDConnect_DotNet.Services;
3434
using WebApp_OpenIDConnect_DotNet.Models;
3535
using WebApp_OpenIDConnect_DotNet.Utils;
3636

2-WebApp-graph-user/2-3-Multi-Tenant/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ For more information about apps and tenancy, see [Tenancy in Azure Active Direct
4343

4444
This sample shows how to build a .NET Core MVC web application that uses the [OpenID Connect](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-protocols-oidc) protocol to sign in users from multiple Azure AD tenants and acquire token for [Microsoft Graph](https://graph.microsoft.com) using the [Microsoft Authentication Library (MSAL)](https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-overview). It leverages the ASP.NET Core OpenID Connect middleware.
4545

46-
The application puts forward a scenario where a SaaS application invites the administrators of Azure AD tenants to `enrol` their tenants into this app. This process is analogous to a customer `buying` a SaaS product.
46+
The application puts forward a scenario where a SaaS application invites the administrators of Azure AD tenants to `enroll` their tenants into this app. This process is analogous to a customer `buying` a SaaS product.
4747

48-
1. Once you start the application, it will ask you to sign-in as an administrator. If its your first time signing-in, you'd land on a page with a heading **Unauthorized Tenant**. Click on the **Take me to the onboarding process** button to onboard your tenant to this application.
49-
1. Once you have **Registered your tenant**
48+
1. Once you start the application, you will land in the homepage where you can sign-in or onboard your tenant.
49+
1. If you sign-in before onboarding your tenant, you'd land on a page with a heading **Unauthorized Tenant**. Click on the **Take me to the onboarding process** button to onboard your tenant to this application.
50+
1. On the onboarding page, you will be asked to sign-in as an **administrator** and accept an **admin consent** for the application.
51+
1. Once you have **registered your tenant**, all users from that tenant will be able to sign-in and explore the todo list.
5052

5153
> Looking for previous versions of this code sample? Check out the tags on the [releases](../../releases) GitHub page.
5254
53-
5455
## How to run this sample
5556

5657
To run this sample:
-2.91 KB
Loading

2-WebApp-graph-user/2-3-Multi-Tenant/BLL/IMSGraphService.cs renamed to 2-WebApp-graph-user/2-3-Multi-Tenant/Services/IMSGraphService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2626
using System.Collections.Generic;
2727
using System.Threading.Tasks;
2828

29-
namespace WebApp_OpenIDConnect_DotNet.BLL
29+
namespace WebApp_OpenIDConnect_DotNet.Services
3030
{
3131
public interface IMSGraphService
3232
{

2-WebApp-graph-user/2-3-Multi-Tenant/BLL/ITodoItemService.cs renamed to 2-WebApp-graph-user/2-3-Multi-Tenant/Services/ITodoItemService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2727
using System.Threading.Tasks;
2828
using WebApp_OpenIDConnect_DotNet.Models;
2929

30-
namespace WebApp_OpenIDConnect_DotNet.BLL
30+
namespace WebApp_OpenIDConnect_DotNet.Services
3131
{
3232
public interface ITodoItemService
3333
{

2-WebApp-graph-user/2-3-Multi-Tenant/BLL/MSGraphService.cs renamed to 2-WebApp-graph-user/2-3-Multi-Tenant/Services/MSGraphService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3131
using System.Net.Http.Headers;
3232
using System.Threading.Tasks;
3333

34-
namespace WebApp_OpenIDConnect_DotNet.BLL
34+
namespace WebApp_OpenIDConnect_DotNet.Services
3535
{
3636
public class MSGraphService : IMSGraphService
3737
{

2-WebApp-graph-user/2-3-Multi-Tenant/BLL/TodoItemService.cs renamed to 2-WebApp-graph-user/2-3-Multi-Tenant/Services/TodoItemService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3232
using WebApp_OpenIDConnect_DotNet.DAL;
3333
using WebApp_OpenIDConnect_DotNet.Models;
3434

35-
namespace WebApp_OpenIDConnect_DotNet.BLL
35+
namespace WebApp_OpenIDConnect_DotNet.Services
3636
{
3737
public class TodoItemService : ITodoItemService
3838
{

2-WebApp-graph-user/2-3-Multi-Tenant/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
using System;
3939
using System.Linq;
4040
using System.Threading.Tasks;
41-
using WebApp_OpenIDConnect_DotNet.BLL;
41+
using WebApp_OpenIDConnect_DotNet.Services;
4242
using WebApp_OpenIDConnect_DotNet.DAL;
4343
using WebApp_OpenIDConnect_DotNet.Utils;
4444

@@ -79,7 +79,7 @@ public void ConfigureServices(IServiceCollection services)
7979

8080
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
8181
{
82-
options.Events.OnTokenValidated = async context =>
82+
options.Events.OnTokenValidated = async context =>
8383
{
8484
string tenantId = context.SecurityToken.Claims.FirstOrDefault(x => x.Type == "tid" || x.Type == "http://schemas.microsoft.com/identity/claims/tenantid")?.Value;
8585

0 commit comments

Comments
 (0)