I am creating a .Net Core application in which a user can login/logout with their microsoft 365 account and also view their Task Lists/Tasks via the Microsoft Graph API. I am currently trying to request the list via the API using.
Controller.cs
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
public async Task<IActionResult> Tasks()
{
User currentUser = null;
Todo currentLists = null;
try
{
currentUser = await _graphServiceClient.Me.Request().GetAsync();
currentLists = await _graphServiceClient.Me.Todo.Lists.Request().GetAsync();
}
catch (ServiceException svcex) when (svcex.Message.Contains("Continuous access evaluation resulted in claims challenge"))
{
try
{
Console.WriteLine($"{svcex}");
string claimChallenge = WwwAuthenticateParameters.GetClaimChallengeFromResponseHeaders(svcex.ResponseHeaders);
_consentHandler.ChallengeUser(_graphScopes, claimChallenge);
return new EmptyResult();
}
catch (Exception ex2)
{
_consentHandler.HandleException(ex2);
}
}
ViewData["Me"] = currentUser;
ViewData["Lists"] = currentLists;
return View();
}
However this throws an issue where
currentLists = await _graphServiceClient.Me.Todo.Lists.Request().GetAsync();
Cannot convert to the Todo type in order for me to then use the data on the following cshtml page
@using Newtonsoft.Json.Linq
@{
ViewData["Title"] = "User Profile fetched from MS Graph";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
<br />
<table class="table table-striped table-condensed" style="font-family: monospace" border="1">
<thead>
<tr>
</tr>
@{
var lists = ViewData["lists"] as Microsoft.Graph.TodoTaskList;
<tr>
<th>
ID
</th>
<th>
@lists.DisplayName
</th>
<th></th>
</tr>
}
</thead>
<tbody>
</tbody>
Where converting currentLists to the type suggested "ITodoListCollectionsPage" causes the web application to now allow access to the data requested.
How can I fix this and call the Microsoft Graph API to get the Todo Task Lists and output that correctly