Skip to content

Commit a038161

Browse files
committed
Updated Readms and Comments
1 parent 2c98b22 commit a038161

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

4-WebApp-your-API/4-3-AnyOrg/Readme.md

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ If you get errors during admin consent, consider deleting the **service princip
376376
### Explore the sample
377377

378378
1. Open your browser and navigate to `https://localhost:44321` and sign-in using the link on top-right.
379-
2. Click on Create New link to create new tasks and you can select the user from the list and assign a task to that user.
380-
3. Click on To-Do List to access all the tasks assigned to users in the tenant of the signed-in user.
379+
1. Click on `To-Do List`, you can click on `Create New` link. It will redirect to create task screen where you can add a new task and assign it to any user from the list.
380+
1. The `To-Do List` screen also displays tasks that are assigned to and created by signed-in user. The user can edit and delete the created tasks but can only view the assigned tasks.
381381

382382
> 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.
383383
@@ -399,11 +399,11 @@ services.AddMicrosoftWebAppAuthentication(Configuration)
399399
.AddInMemoryTokenCaches();
400400
```
401401

402-
1. AddMicrosoftWebAppAuthentication :TODO: add details
403-
1. AddMicrosoftWebAppCallsWebApi :TODO: add details
404-
1. AddInMemoryTokenCaches:TODO: add details
402+
1. AddMicrosoftWebAppAuthentication : 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.
403+
1. AddMicrosoftWebAppCallsWebApi : Enables the web app to call protected APIs.
404+
1. AddInMemoryTokenCaches: Adds an in memory token cache provider, which will cache the Access Tokens acquired for the Web API.
405405

406-
The following code injects the ToDoList service implementation in the client
406+
The following code enables to add client service to use the HttpClient by dependency injection.
407407

408408
```CSharp
409409
services.AddTodoListService(Configuration);
@@ -439,8 +439,9 @@ public IActionResult AdminConsentClient()
439439

440440
### Handle the **MsalUiRequiredException** from Web API
441441

442-
In `ToDoListService.cs`, the method `HandleChallengeFromWebApi` handles the `MsalUiRequiredException` response from Web API in the on-behalf of flow. It creates a consent URI and throws a custom exception i.e., `WebApiMsalUiRequiredException`.
443-
TODO: Explain why it is needed?
442+
If signed-in user does not have consent for a permission on the Web API, for instance "user.read.all" in this sample, then Web API will throw `MsalUiRequiredException`. The response contains the details about consent Uri and proposed action.
443+
444+
The Web App contains a method `HandleChallengeFromWebApi` in `ToDoListService.cs` that handles the exception thrown by API. It creates a consent URI and throws a custom exception i.e., `WebApiMsalUiRequiredException`.
444445

445446
```csharp
446447
private void HandleChallengeFromWebApi(HttpResponseMessage response)
@@ -473,9 +474,7 @@ private void HandleChallengeFromWebApi(HttpResponseMessage response)
473474
}
474475
```
475476

476-
The following code in `ToDoListController.cs` catches the `WebApiMsalUiRequiredException` exception and redirects to consent Uri.
477-
478-
TODO: Explain why it is needed?
477+
The following code in `ToDoListController.cs` catches the `WebApiMsalUiRequiredException` exception thrown by `HandleChallengeFromWebApi` method as explained above. Further it Redirects to `consentUri` that is retrieved from exception message. Admin needs to consent as `user.read.all` permission requires admin approval.
479478

480479
```csharp
481480
public async Task<IActionResult> Create()
@@ -487,7 +486,6 @@ public async Task<IActionResult> Create()
487486
}
488487
catch (WebApiMsalUiRequiredException ex)
489488
{
490-
var a = ex.Message;
491489
return Redirect(ex.Message);
492490
}
493491
}
@@ -497,14 +495,30 @@ public async Task<IActionResult> Create()
497495

498496
#### Admin consent Client Redirect
499497

500-
In HomeController.cs, the method `AdminConsent` redirects to the URI passed in the state parameter by Web App.
498+
In HomeController.cs, the method `AdminConsent` redirects to the URI passed in the state parameter by Web App. If admin consent is cancelled from API consent screen then it redirects to base address of Web App.
501499

502500
```csharp
503501
public IActionResult AdminConsent()
504502
{
505-
var queryString = System.Web.HttpUtility.ParseQueryString(HttpContext.Request.QueryString.ToString());
503+
var decodeUrl = System.Web.HttpUtility.UrlDecode(HttpContext.Request.QueryString.ToString());
504+
var queryString = System.Web.HttpUtility.ParseQueryString(decodeUrl);
506505
var clientRedirect = queryString["state"];
507-
return Redirect(clientRedirect);
506+
if (!string.IsNullOrEmpty(clientRedirect))
507+
{
508+
if (queryString["error"] == "access_denied" && queryString["error_subcode"] == "cancel")
509+
{
510+
var clientRedirectUri = new Uri(clientRedirect);
511+
return Redirect(clientRedirectUri.GetLeftPart(System.UriPartial.Authority));
512+
}
513+
else
514+
{
515+
return Redirect(clientRedirect);
516+
}
517+
}
518+
else
519+
{
520+
return RedirectToAction("GetTodoItems", "TodoList");
521+
}
508522
}
509523
```
510524

4-WebApp-your-API/4-3-AnyOrg/ToDoListClient/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public void ConfigureServices(IServiceCollection services)
3434
options.HandleSameSiteCookieCompatibility();
3535
});
3636

37-
// TODO: Comment properly
37+
//Add authentication with Microsoft identity platform.
3838
services.AddMicrosoftWebAppAuthentication(Configuration)
3939
.AddMicrosoftWebAppCallsWebApi(Configuration, new string[] { Configuration["TodoList:TodoListScope"] })
4040
.AddInMemoryTokenCaches();
4141

42-
// TODO: Comment properly
42+
//Enables to add client service to use the HttpClient by dependency injection.
4343
services.AddTodoListService();
4444

4545
services.AddControllersWithViews(options =>

0 commit comments

Comments
 (0)