Closed hannespreishuber closed 3 years ago
@hannespreishuber The AuthorizationCodeProvider is intended to be used with the Auth Code Flow.Therefore, web app will need to get an Auth Code to use in order to get an access token. You are most likely getting this error since you have not yet acquired an access token the right way.
If you check out the documentation here, you will see that you need to modify your StartUp.cs so as to add an EventHandler when an AuthCode is received. The event handler can then call this method from the AuthProvider so that you can now acquire an access token and make it available for the app.
You can also find samples using the Auth Code flow here to call Microsft graph.
to much links ... did already
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification context)
{
// Upon successful sign-in, get the access token and cache it by using MSAL.
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(System.Configuration.ConfigurationManager.AppSettings["ClientId"])
.WithClientSecret(System.Configuration.ConfigurationManager.AppSettings["ClientSecret"])
.WithTenantId(System.Configuration.ConfigurationManager.AppSettings["Tenant"])
.WithRedirectUri(System.Configuration.ConfigurationManager.AppSettings["redirectUri"])
.WithAuthority(System.Configuration.ConfigurationManager.AppSettings["Authority"])
.Build();
AuthenticationResult result = await confidentialClientApplication.AcquireTokenByAuthorizationCode(new[] { "User.Read.All" }, context.Code).ExecuteAsync();
but no idea where to add the token
also within controller
var tokenResult = cca.AcquireTokenForClient(new List<string> { "https://graph.microsoft.com/.default" });
var token = await tokenResult.ExecuteAsync();
feels like a big puzzle with 10000 pieces. Just want 10 lines of code...
just to complete the whole picture, what I tried already manual paste a token to the sdk request header- always the same result...
var ho = new HeaderOption("Authorization", "Bearer "+ token.AccessToken);
var holist = new List<HeaderOption>();
holist.Add(ho);
var user = await graphClient.Me
.Request(holist)
.GetAsync();
return View();```
Hi @hannespreishuber
Thank you for reaching out and opening this issue. This client library will not leave the preview state. Microsoft.Graph v4 now integrates with Azure.Identity which supports a wide variety of authentication flows out of the box. We suggest that you migrate to v4 + Azure.Identity. Read more about it in this issue.
This issue won't be fixed, and the repository will be archived.
done at least 10 hours of ASP.NET MVC there is no complete sample for AuthorizationCodeProvider
so my code
get error message
Code: authenticationChallengeRequired Message: Authentication challenge is required. Beschreibung: Unbehandelte Ausnahme beim Ausführen der aktuellen Webanforderung. Überprüfen Sie die Stapelüberwachung, um weitere Informationen über diesen Fehler anzuzeigen und festzustellen, wo der Fehler im Code verursacht wurde.
Ausnahmedetails: Microsoft.Graph.Auth.AuthenticationException: Code: authenticationChallengeRequired Message: Authentication challenge is required.
AB#8392