Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to do this with Teams Toolkit #27432

Closed
wrharper opened this issue Oct 29, 2022 · 10 comments
Closed

Unable to do this with Teams Toolkit #27432

wrharper opened this issue Oct 29, 2022 · 10 comments
Assignees
Labels
Blazor Source - Docs.ms Docs Customer feedback via GitHub Issue support-request

Comments

@wrharper
Copy link

This gives errors is you implement this with a brand new teams toolkit visual studio 2022 project. So how do you do graph api with teams toolkit?


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

@dotnet-bot dotnet-bot added Blazor Source - Docs.ms Docs Customer feedback via GitHub Issue labels Oct 29, 2022
@guardrex
Copy link
Collaborator

Hello @ziron999 ... I'm not familiar with "Teams Toolkit," nor do I understand why any tooling in VS would have anything to do with the coverage in the article.

Beyond that, this is an older article that will be receiving work (or replacement with an Azure document on MS Graph) in the holiday period next month into December, roughly between 🦃 Thanksgiving and the holidays 🎅🎆. That work is tracked by #24615, and I hope to have the work done by the EOY.

You're welcome to post the errors that you're seeing here for me to take a look at. If there's any work to be done, it would likely be performed a little later due to the upcoming release of .NET 7 in mid-November.

@wrharper
Copy link
Author

ok, i will as soon as i can. this is pretty critical for me to be able to make apps correctly in Teams.

@wrharper
Copy link
Author

Error with no provider:
System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Graph.IAuthenticationProvider Lifetime: Scoped ImplementationType: GraphClientExtensions+NoOpGraphAuthenticationProvider': Unable to resolve service for type 'Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider' while attempting to activate 'GraphClientExtensions+NoOpGraphAuthenticationProvider'.)'

Error with provider shown in docs:
System.InvalidCastException: 'Unable to cast object of type 'Microsoft.AspNetCore.Components.Server.ServerAuthenticationStateProvider' to type 'Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider'.'
and this happens in the GraphExetensions.cs here:
services.AddScoped(sp =>
{
return new GraphServiceClient(
sp.GetRequiredService(),
sp.GetRequiredService());
});

@wrharper
Copy link
Author

Everything is just as it shows in the doc at the start.
Program.cs has this added to it:
builder.Services.AddMsalAuthentication<RemoteAuthenticationState,
RemoteUserAccount>(options =>
{
builder.Configuration.Bind("AzureAd",
options.ProviderOptions.Authentication);
options.ProviderOptions.DefaultAccessTokenScopes.Add(
"https://graph.microsoft.com/User.Read");
})
.AddAccountClaimsPrincipalFactory<RemoteAuthenticationState, RemoteUserAccount,
CustomAccountFactory>();

builder.Services.AddGraphClient("https://graph.microsoft.com/User.Read");

@wrharper
Copy link
Author

scope is valid:
var result = await TokenProvider.RequestAccessToken(
new AccessTokenRequestOptions()
{
Scopes = new[] { "https://graph.microsoft.com/User.Read" }
});

@guardrex
Copy link
Collaborator

This article only applies to standalone or hosted Blazor WebAssembly apps. If you're seeking to implement MS Graph in a Blazor Server app, then you follow guidance for implementing Graph API in a regular ASP.NET Core app.

There's a tutorial in the Azure docs at ...

https://learn.microsoft.com/azure/active-directory/develop/tutorial-blazor-server

... where they have a different project template (blazorserver2) that comes from installing a special set of project templates in Microsoft.Identity.Web.ProjectTemplates. It looks like the article and the sample is 3.1 era tho.

Their sample apps can be found here ...

https://learn.microsoft.com/azure/active-directory/develop/sample-v2-code#web-applications

... and they have a Call MS Graph sample app at ...

https://github.com/Azure-Samples/ms-identity-blazor-server/tree/main/WebApp-graph-user/Call-MSGraph

... but that's 3.1 era, too.

Here's a good one to look at: I found a recent sample over there for 6.0 🎉 at ...

https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/2-WebApp-graph-user/2-1-Call-MSGraph

I'd look at that one the closest. I think they may have missed updating their samples page link. When I reach my issue for work, I might open an issue with them to inquire on it. Anyway, that looks like the best one that they have.

Note that their sample doesn't use Minimal APIs, so you'll see the ancient 👴😄 Program.cs/Startup.cs bits. That's fine tho ... that approach to organizing startup code is still valid. Anyway , you'll see the right authn/z services setup and config for a 6.0 era app and should be able to translate that into a Minimal APIs-based app or get the right calls into the right spots in your existing Minimal APIs app.

There's also going to be new ASP.NET Core templates for 7.0 that have built-in Graph API. I haven't looked at them closely yet for how they enable/integrate Graph API, but it should be simpler to wire up Graph going forward. The new templates are in preview now and will be released along with the rest of .NET 7 in November.

One of things that I'll be doing on my updates to our Blazor Security node is get the best cross-links into our docs. The tutorials and sample apps that you see over there, along with a great deal of new MS Identity Platform and Azure hosting docs, were put up after all of the docs in this node were written. We probably will be able to drop and cross-link to their resources for these types of authn/z with Azure and Graph scenarios.

Side Note: For Blazor WebAssembly, they have a similar tutorial/samples for it in the same areas of their docs. Their 6.0 samples are more current than ours over here, too, for Blazor WebAssembly. I'd recommend that devs working on Blazor WebAssembly apps also look at their docs and samples more closely than ours over here ... at least until I can sort out this node with updates and cross-links later this year.

I'm going to close this as not actionable at this time, but I will cross-link this issue/discussion on my tracking issue to review it in November/December. If you require product support, we recommend the usual public support channels ...

@wrharper
Copy link
Author

Thanks, I will look into these solutions on Monday. I have yet to get anything working correctly with this stuff because of all the misinformation and wrong docs. It has been difficult to find an actual working starting point so I can just start doing my normal programming. It is very confusing how all this is structured to me, and C# is my main language. This isn't even recognizable as normal to me at all lol. Why do claims even need to be a thing for example? What's the point of that? Why isn't this just simple RESTful POST/GET? It seems like it has been overly complicated on design.

@wrharper
Copy link
Author

oh also, i need to mention that it must be blazor and a lot of these docs are about swagger as well which are all useless for me. The teams toolkit is based on blazor only.

@guardrex
Copy link
Collaborator

guardrex commented Oct 30, 2022

misinformation and wrong docs

But if someone uses a doc written for Blazor WebAssembly to implement an API for Blazor Server, then that wouldn't be expected to work. I'm still not 100% clear on what hosting model you're using. You never said, but that error message makes reference to Blazor Server API, so that's what I assumed you were using.

I recommend going by the Azure docs that I mentioned, paying the most attention to that 6.0 Blazor Server sample app. I'll see about fixing up (or dropping 🔪 and cross-linking where I can) the Blazor node docs to what they have, including adding cross-links for those 6.0 Azure/AAD/Graph samples.

WRT ...

Why do claims even need to be a thing for example?

has been overly complicated on design

Why isn't this just simple RESTful POST/GET?

Those sorts of considerations are mostly out of scope for our work at this level. You're addressing industry-wide (OAuth 2.0/OIDC) and lower-level Microsoft platform features (MS Identity Platform).

WRT what is in scope here for how to implement the protocols and platform features, I won't know more about how our Blazor security node docs need to change until I reach the node for work in a few weeks. I was hoping to do it last year, which you can see on #19286. It wasn't reached 😢 ... just too 🏃🏃🏃 to reach this node last year. AFAICT, I will reach it this year. Still tho, the Blazor WebAssembly node docs can't be directly used for these scenarios in a Blazor Server apps, even after I seek to improve them.

oh also, i need to mention that it must be blazor and a lot of these docs are about swagger as well which are all useless for me. The teams toolkit is based on blazor only.

I don't understand. There's no Swagger in the Blazor docs. I think you're referring to the main doc set articles.

WRT tooling: There's nothing specific about the tooling that prevents the guidance here from working AFAIK. There was a problem with how VS was creating Blazor apps with AAD/B2C auth, but I think they fixed it in VS 2019 for .NET 6's release. Anyway, the Blazor security docs don't use VS. They use the .NET CLI.

I'll take a closer look at the Teams Toolkit. I don't know anything about it at this point, so I'll need to do some research on it. I wouldn't think that your issues have anything to do with it, but I can't say for sure not knowing what it is (yet! 😄).

@wrharper
Copy link
Author

Turns out, none of this is needed and I figure out how to do with the TeamsFx SDK. It is already tied like how it should be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blazor Source - Docs.ms Docs Customer feedback via GitHub Issue support-request
Projects
Archived in project
Development

No branches or pull requests

3 participants