Skip to content

Commit 7511453

Browse files
author
Tiago Brenck
committed
Adding code comments
1 parent 305659b commit 7511453

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public HomeController(SampleDbContext dbContext)
4646

4747
public IActionResult Index()
4848
{
49+
//Getting all authorized tenants to be displayed on a table, for demonstration purpose
4950
var authorizedTenants = dbContext.AuthorizedTenants.Where(x => x.TenantId != null && x.AuthorizedOn != null).ToList();
5051
return View(authorizedTenants);
5152
}
@@ -58,6 +59,7 @@ public IActionResult DeleteTenant(string id)
5859

5960
var signedUserTenant = User.GetTenantId();
6061

62+
// If the user deletes its own tenant from the list, they should be signed-out
6163
if (id == signedUserTenant)
6264
return RedirectToAction("SignOut", "Account", new { area = "AzureAD" });
6365
else
@@ -67,6 +69,7 @@ public IActionResult DeleteTenant(string id)
6769
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
6870
public IActionResult UnauthorizedTenant()
6971
{
72+
//If you landed here, is because you tried to sign-in with a user from a tenant that hasnt been onboarded in the application yet.
7073
return View();
7174
}
7275

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public IActionResult Onboard()
6767
TempAuthorizationCode = stateMarker //Use the stateMarker as a tempCode, so we can find this entity on the ProcessCode method
6868
};
6969

70+
// Saving a temporary tenant to validate the stateMarker on the admin consent response
7071
dbContext.AuthorizedTenants.Add(authorizedTenant);
7172
dbContext.SaveChanges();
7273

@@ -79,11 +80,10 @@ public IActionResult Onboard()
7980
string authorizationRequest = string.Format(
8081
"{0}common/v2.0/adminconsent?client_id={1}&redirect_uri={2}&state={3}&scope={4}",
8182
azureADOptions.Instance,
82-
Uri.EscapeDataString(azureADOptions.ClientId),
83-
Uri.EscapeDataString(currentUri + "Onboarding/ProcessCode"),
84-
Uri.EscapeDataString(stateMarker),
85-
Uri.EscapeDataString("https://graph.microsoft.com/.default"));
86-
83+
Uri.EscapeDataString(azureADOptions.ClientId), // The application Id on Azure Portal
84+
Uri.EscapeDataString(currentUri + "Onboarding/ProcessCode"), //Uri that will get redirected after the admin has consented
85+
Uri.EscapeDataString(stateMarker), // The state parameter is used to validate the response, preventing a man-in-the-middle attack, and it will also be used to identify the entity on ProcessCode action.
86+
Uri.EscapeDataString("https://graph.microsoft.com/.default")); // The scopes to be presented to the admin. Here we are using the static scope /.default.
8787

8888
return Redirect(authorizationRequest);
8989
}
@@ -99,7 +99,7 @@ public async Task<IActionResult> ProcessCode(string tenant, string error, string
9999

100100
var authenticationProperties = new AuthenticationProperties { RedirectUri = "Home/Index" };
101101

102-
// Check if tenant is already authorized
102+
// If tenant is already authorized, there is no need updated its record
103103
if (dbContext.AuthorizedTenants.FirstOrDefault(x => x.TenantId == tenant) != null)
104104
{
105105
// Challenge an authentication so dotnet can set the user identity claims.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public async Task<IEnumerable<User>> GetUsersAsync(string accessToken)
5252
try
5353
{
5454
PrepareAuthenticatedClient(accessToken);
55+
56+
// Using Graph SDK to get users, filtering by active ones and returning just id and userPrincipalName field
5557
users = await graphServiceClient.Users.Request()
5658
.Filter($"accountEnabled eq true")
5759
.Select("id, userPrincipalName")

0 commit comments

Comments
 (0)