Skip to content

Commit

Permalink
Update test to use IDownstreamApi (#2198)
Browse files Browse the repository at this point in the history
* update sample to use IDownstreamApi
  • Loading branch information
westin-m authored Apr 14, 2023
1 parent aff223a commit 9b5dce9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 187 deletions.
165 changes: 0 additions & 165 deletions tests/DevApps/blazorserver-calls-api/Client/Data/ToDoListService.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
@page "/create"

@using ToDoListModel
@using Microsoft.Identity.Abstractions

@inject ToDoListService ToDoListService
@inject NavigationManager Navigation
@inject IDownstreamApi _downstreamApi

<h3>Create Task</h3>

<CommonForm ButtonText="Add Task" ToDoItem="@toDo"
OnValidSubmit="@AddTask" />
OnValidSubmit="@AddTask" />

@code {
protected ToDo toDo = new ToDo();

protected async Task AddTask()
{
await ToDoListService.AddAsync(toDo);
await _downstreamApi.PostForUserAsync("TodoList", toDo);
Navigation.NavigateTo("todolist");
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
@page "/edit/{Id:int}"

@using ToDoListModel
@using Microsoft.Identity.Abstractions

@inject ToDoListService ToDoListService
@inject NavigationManager Navigation
@inject IDownstreamApi _downstreamApi

<h3>Edit Task</h3>

Expand All @@ -15,11 +16,16 @@
ToDo toDo = new ToDo();
protected async override Task OnParametersSetAsync()
{
toDo = await ToDoListService.GetAsync(Id);
toDo = await _downstreamApi.GetForUserAsync<ToDo>(
"TodoList",
options => options.RelativePath = $"api/todolist/{Id}");
}

protected async Task EditTask()
{
await ToDoListService.EditAsync(toDo);
await _downstreamApi.PatchForUserAsync<ToDo, ToDo>(
"TodoList", toDo,
options => options.RelativePath = $"api/todolist/{Id}");
Navigation.NavigateTo("todolist");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Http;
using Microsoft.Identity.Abstractions;
using Microsoft.Identity.Web;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using ToDoListModel;

Expand All @@ -10,24 +13,22 @@ namespace blazorserver_client.Pages.ToDoPages

public class ToDoListBase : ComponentBase
{
[Inject]
ToDoListService ToDoListService { get; set; }

[Inject]
MicrosoftIdentityConsentAndConditionalAccessHandler ConsentHandler { get; set; }

[Inject]
NavigationManager Navigation { get; set; }
IDownstreamApi _downstreamApi { get; set; }

protected IEnumerable<ToDo> toDoList = new List<ToDo>();

protected ToDo toDo = new ToDo();

protected override async Task OnInitializedAsync()
{
await GetToDoListService();
}

/// <summary>
/// Gets all todo list items.
/// </summary>
Expand All @@ -37,7 +38,7 @@ private async Task GetToDoListService()
{
try
{
toDoList = await ToDoListService.GetAsync();
toDoList = await _downstreamApi.GetForUserAsync<IEnumerable<ToDo>>("TodoList");
}
catch (Exception ex)
{
Expand All @@ -55,7 +56,9 @@ private async Task GetToDoListService()
/// <returns></returns>
protected async Task DeleteItem(int Id)
{
await ToDoListService.DeleteAsync(Id);
await _downstreamApi.DeleteForUserAsync<ToDo>(
"TodoList", toDo,
options => options.RelativePath = $"api/todolist/{Id}");
await GetToDoListService();
}
}
Expand Down
15 changes: 7 additions & 8 deletions tests/DevApps/blazorserver-calls-api/Client/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -36,14 +33,16 @@ public void ConfigureServices(IServiceCollection services)
// Add authentication with Microsoft identity platform.
// EnableTokenAcquisitionToCallDownstreamApi adds support for the web app to acquire tokens to call the web API.
services.AddMicrosoftIdentityWebAppAuthentication(Configuration)
.EnableTokenAcquisitionToCallDownstreamApi(new string[] { Configuration["TodoList:TodoListScope"] })
.AddInMemoryTokenCaches(); ;

.EnableTokenAcquisitionToCallDownstreamApi(
Configuration.GetSection("TodoList").GetValue<string[]>("Scopes")
)
.AddInMemoryTokenCaches();

services.AddHttpContextAccessor();

// Enables to add client service to use the HttpClient by dependency injection.
services.AddToDoListService(Configuration);
services.AddDownstreamApi("TodoList", Configuration.GetSection("TodoList"));

services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\src\Microsoft.Identity.Web.DownstreamApi\Microsoft.Identity.Web.DownstreamApi.csproj" />
<ProjectReference Include="..\..\..\..\src\Microsoft.Identity.Web.UI\Microsoft.Identity.Web.UI.csproj" />
<ProjectReference Include="..\..\..\..\src\Microsoft.Identity.Web\Microsoft.Identity.Web.csproj" />
<ProjectReference Include="..\ToDoListModel\ToDoListModel.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Data\" />
</ItemGroup>

</Project>

0 comments on commit 9b5dce9

Please sign in to comment.