Meilleur auteur de réponses
Récupération du service OAuth dans un contrôleur

Question
-
Bonjour à tous,
Je copie ici un message posté sur d'autres forums. Merci d'excuser de ne pas avoir traduit.
------------------
In ASP.Net MVC Core 2, we are trying to call the Linkedin web API with OAuth authentication.
We are able to declare the OAuth authentication service and retrieve the access token the from Linkedin as shown in the code below.
Now we would like to request the API from a controller. To do that, we have to get the access token from the OAuth service we have declared with the AddOAuth method. How can we do that? No way to find an example anywhere.
Thanx for your help, we are really stuck.
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie("linkedin_login", options => { options.LoginPath = new PathString("/login"); // "/Account/LogIn"; options.LogoutPath = new PathString("/logout"); // "/Account/LogOff"; }) .AddOAuth("LinkedIn", options => { options.SignInScheme = "linkedin_login"; options.ClientId = Configuration["linkedin:clientId"]; options.ClientSecret = Configuration["linkedin:clientSecret"]; options.CallbackPath = new PathString("/signin-linkedin");// "/signin-linkedin"; options.AuthorizationEndpoint = "https://www.linkedin.com/oauth/v2/authorization"; options.TokenEndpoint = "https://www.linkedin.com/oauth/v2/accessToken"; options.UserInformationEndpoint = "https://api.linkedin.com/v1/people/~:(id,first-name,last-name,formatted-name,email-address,picture-url,picture-urls,headline,public-profile-url,industry,three-current-positions,three-past-positions,positions::(original))"; // // To save the tokens to the Authentication Properties we need to set this to true // See code in OnTicketReceived event below to extract the tokens and save them as Claims options.SaveTokens = true;//JCV News !! options.Scope.Add("r_basicprofile"); options.Scope.Add("r_emailaddress"); options.Events = new OAuthEvents { OnCreatingTicket = async context => { #region OnCreatingTicket // Retrieve user info var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken); request.Headers.Add("x-li-format", "json"); // Tell LinkedIn we want the result in JSON, otherwise it will return XML //… blah blah blah…
Searching... wondering...
Réponses
-
Et entre temps, on a trouvé : GetTokenAsync sur HttpContext. 2 jours pour ça... :(
- Proposé comme réponse Patrice Ongla (pro) vendredi 17 novembre 2017 00:18
- Marqué comme réponse Nina ZaekovaMicrosoft contingent staff, Moderator jeudi 7 décembre 2017 11:54
Toutes les réponses
-
Bonjour,
Merci d’avoir contacté les forums MSDN France. La langue utilisée sur ces forums est la langue Française, donc s’il vous plaît modifiez votre premier message, comme on vous demande dans l’étiquette sur les forums MSDN France.
Merci pour votre compréhension et collaboration.
Je partage le lien vers votre question en parallèle pour une information supplémentaire:
How to get token well retrieved in services into controller (Core 2.0 oAuth 2) ?
Cordialement,
NinaMicrosoft propose ce service gratuitement, dans le but d'aider les utilisateurs et d'élargir les connaissances générales liées aux produits et technologies Microsoft. Ce contenu est fourni "tel quel" et il n'implique aucune responsabilité de la part de Microsoft.
-
Et voilà (merci Deepl :) ).
_______
Dans ASP. Net MVC Core 2, nous essayons d'appeler l'API Web Linkedin avec authentification OAuth.
Nous sommes en mesure de déclarer le service d'authentification OAuth et de récupérer le jeton d'accès depuis Linkedin comme indiqué dans le code ci-dessous.
Nous aimerions maintenant demander l'API à un contrôleur. Pour ce faire, nous devons obtenir le jeton d'accès du service OAuth que nous avons déclaré avec la méthode AddOAuth. Comment pouvons-nous faire cela? Aucun moyen de trouver un exemple nulle part.
Merci pour votre aide, nous sommes vraiment coincés. -
Et entre temps, on a trouvé : GetTokenAsync sur HttpContext. 2 jours pour ça... :(
- Proposé comme réponse Patrice Ongla (pro) vendredi 17 novembre 2017 00:18
- Marqué comme réponse Nina ZaekovaMicrosoft contingent staff, Moderator jeudi 7 décembre 2017 11:54