Skip to content

Commit 1046522

Browse files
committed
Update to display tenant specific tasks
1 parent d45d76a commit 1046522

File tree

8 files changed

+15
-23
lines changed

8 files changed

+15
-23
lines changed

4-WebApp-your-API/4-3-AnyOrg/ToDoListClient/Controllers/ToDoListController.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@
1111

1212
namespace ToDoListClient.Controllers
1313
{
14-
[Authorize]
14+
[AuthorizeForScopes(ScopeKeySection = "TodoList:TodoListScope")]
1515
public class ToDoListController : Controller
1616
{
1717
private IToDoListService _todoListService;
18-
1918
public ToDoListController(IToDoListService todoListService)
2019
{
2120
_todoListService = todoListService;
22-
2321
}
2422

25-
[AuthorizeForScopes(ScopeKeySection = "TodoList:TodoListScope")]
2623
// GET: TodoList
2724
public async Task<ActionResult> Index()
2825
{
@@ -43,18 +40,18 @@ public async Task<IActionResult> Create()
4340
{
4441
Text = u
4542
}).ToList();
43+
TempData["TenantId"] = HttpContext.User.GetTenantId();
4644
return View(todo);
4745
}
4846
catch (WebApiMsalUiRequiredException ex)
4947
{
50-
var a = ex.Message;
5148
return Redirect(ex.Message);
5249
}
5350
}
5451
// POST: TodoList/Create
5552
[HttpPost]
5653
[ValidateAntiForgeryToken]
57-
public async Task<ActionResult> Create([Bind("Title,Owner")] ToDoItem todo)
54+
public async Task<ActionResult> Create([Bind("Title,Owner,TenantId")] ToDoItem todo)
5855
{
5956
await _todoListService.AddAsync(todo);
6057
return RedirectToAction("Index");
@@ -76,7 +73,7 @@ public async Task<ActionResult> Edit(int id)
7673
// POST: TodoList/Edit/5
7774
[HttpPost]
7875
[ValidateAntiForgeryToken]
79-
public async Task<ActionResult> Edit(int id, [Bind("Id,Title,Owner")] ToDoItem todo)
76+
public async Task<ActionResult> Edit(int id, [Bind("Id,Title,Owner,TenantId")] ToDoItem todo)
8077
{
8178
await _todoListService.EditAsync(todo);
8279
return RedirectToAction("Index");

4-WebApp-your-API/4-3-AnyOrg/ToDoListClient/Models/ToDoItem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ public class ToDoItem
1212
public string Title { get; set; }
1313

1414
public string Owner { get; set; }
15+
public string TenantId { get; set; }
1516
}
1617
}

4-WebApp-your-API/4-3-AnyOrg/ToDoListClient/Services/ToDoListService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace ToDoListClient.Services
1818
{
1919
public static class TodoListServiceExtensions
2020
{
21-
public static void AddTodoListService(this IServiceCollection services, IConfiguration configuration)
21+
public static void AddTodoListService(this IServiceCollection services)
2222
{
2323
// https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests
2424
services.AddHttpClient<IToDoListService, ToDoListService>();
@@ -29,7 +29,6 @@ public class ToDoListService : IToDoListService
2929
private readonly HttpClient _httpClient;
3030
private readonly string _TodoListScope = string.Empty;
3131
private readonly string _TodoListBaseAddress = string.Empty;
32-
private readonly string _ClientId = string.Empty;
3332
private readonly string _RedirectUri = string.Empty;
3433
private readonly string _ApiRedirectUri = string.Empty;
3534
private readonly ITokenAcquisition _tokenAcquisition;
@@ -40,7 +39,6 @@ public ToDoListService(ITokenAcquisition tokenAcquisition, HttpClient httpClient
4039
_tokenAcquisition = tokenAcquisition;
4140
_TodoListScope = configuration["TodoList:TodoListScope"];
4241
_TodoListBaseAddress = configuration["TodoList:TodoListBaseAddress"];
43-
_ClientId = configuration["AzureAd:ClientId"];
4442
_RedirectUri = configuration["RedirectUri"];
4543
_ApiRedirectUri = configuration["TodoList:AdminConsentRedirectApi"];
4644
if (!string.IsNullOrEmpty(_TodoListBaseAddress))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void ConfigureServices(IServiceCollection services)
3636
services.AddMicrosoftWebAppAuthentication(Configuration)
3737
.AddMicrosoftWebAppCallsWebApi(Configuration, new string[] { Configuration["TodoList:TodoListScope"] })
3838
.AddInMemoryTokenCaches();
39-
services.AddTodoListService(Configuration);
39+
services.AddTodoListService();
4040
services.AddControllersWithViews(options =>
4141
{
4242
var policy = new AuthorizationPolicyBuilder()

4-WebApp-your-API/4-3-AnyOrg/ToDoListClient/Views/ToDoList/Create.cshtml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@
1010
<div class="row">
1111
<div class="col-md-4">
1212
<form asp-action="Create">
13+
<input type="hidden" asp-for="TenantId" value="@((string)TempData["TenantId"])"/>
1314
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
1415
<div class="form-group">
1516
<label asp-for="Title" class="control-label"></label>
1617
<input asp-for="Title" class="form-control" />
1718
<span asp-validation-for="Title" class="text-danger"></span>
1819
</div>
1920
<div class="form-group">
20-
@*<label asp-for="Owner" class="control-label"></label>
21-
<input asp-for="Owner" class="form-control" value="@Html.DisplayTextFor(model => model.Owner)"/>
22-
<span asp-validation-for="Owner" class="text-danger"></span>*@
23-
<label asp-for="Owner" class="control-label"></label>
21+
<label asp-for="Owner" class="control-label"></label>
2422

2523
<select class="form-control" asp-for="Owner" asp-items="@((List<SelectListItem>)TempData["UsersDropDown"])"></select>
2624

4-WebApp-your-API/4-3-AnyOrg/ToDoListClient/Views/ToDoList/Edit.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<form asp-action="Edit">
1010
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
1111
<input type="hidden" asp-for="Id" />
12+
<input type="hidden" asp-for="TenantId"/>
1213
<div class="form-group">
1314
<label asp-for="Title" class="control-label"></label>
1415
<input asp-for="Title" class="form-control" />

4-WebApp-your-API/4-3-AnyOrg/TodoListService/Controllers/TodoListController.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@ public TodoListController(TodoContext context,ITokenAcquisition tokenAcquisition
3939
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
4040
{
4141
HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi);
42+
string userTenantId = HttpContext.User.GetTenantId();
4243
try
4344
{
4445
Microsoft.Graph.User user = new User();
4546
await _context.TodoItems.ToListAsync();
4647
}
47-
catch(Exception ex)
48+
catch(Exception)
4849
{
49-
var a = ex.Message;
50+
throw;
5051
}
51-
return await _context.TodoItems.ToListAsync();
52+
return await _context.TodoItems.Where(x => x.TenantId == userTenantId).ToListAsync();
5253
}
5354

5455
// GET: api/TodoItems/5

4-WebApp-your-API/4-3-AnyOrg/TodoListService/Models/TodoItem.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ public class TodoItem
1212
public string Title { get; set; }
1313

1414
public string Owner { get; set; }
15+
public string TenantId { get; set; }
1516
}
16-
//public class Users
17-
//{
18-
// public int UserId { get; set; }
19-
// public string UserName { get; set; }
20-
//}
2117
}

0 commit comments

Comments
 (0)