In ASP.Net MVC Core 2 versuchen wir, die Linkedin-Web-API mit OAuth-Authentifizierung aufzurufen. Wir können den OAuth-Authentifizierungsdienst deklarieren und das Zugriffstoken von Linkedin abrufen, wie im folgenden Code gezeigt. Nun möchten wir die API von einem Controller anfordern. Dazu müssen wir das Zugriffstoken aus dem OAuth-Dienst abrufen, den wir mit der AddOAuth-Methode deklariert haben. Wie können wir das machen? Keine Möglichkeit, irgendwo ein Beispiel zu finden.Wie bekomme ich Token in Services gut in Controller (Core 2.0 oAuth 2)?
Danke für Ihre Hilfe, wir sind wirklich fest.
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie("linkedin_login", options =>
{
options.LoginPath = new PathString("/login");
options.LogoutPath = new PathString("/logout");
})
.AddOAuth("LinkedIn", options =>
{
options.SignInScheme = "linkedin_login";
options.ClientId = Configuration["linkedin:clientId"];
options.ClientSecret = Configuration["linkedin:clientSecret"];
options.CallbackPath = new PathString("/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;
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);
// Wir haben hier die Token: context.AccessToken request.Headers.Add ("x-li-Format", "json"); // Sag LinkedIn wir das Ergebnis in JSON wollen, sonst wird es zurückgeben XML