0

技术栈

  • 使用 .NET CORE React 模板
  • OAuth - 领英身份验证
  • NPM 我反应饼干
  • 1 IIS 网站
  • 应用程序池(v4 集成)
  • 端口 80

问题

LinkedIn 身份验证成功后,在 UI 端,以下两行都返回'undefined'

console.debug("设置", "渲染", "050", cookies.Name);

console.debug("设置", "render", "075", cookies['Name']);

设置.tsx

import React, { FC, ReactElement, useState, useEffect } from "react";
import { useCookies } from 'react-cookie';


    const [cookies, setCookies] = useCookies(['Id','Name']);

    const sourceId = cookies.Name;

    console.debug("Settings", "render", "050", cookies.Name);
    console.debug("Settings", "render", "075", cookies['Name']);

Start.cs ConfigureServices(IServiceCollection 服务)


            services.AddAuthentication(o =>
            {
                o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                o.DefaultChallengeScheme = "LinkedIn";
            })
               .AddCookie(o =>
               {
                   o.LoginPath = new PathString("/account/login");
                   o.LogoutPath = new PathString("/account/logout");
               })
               .AddOAuth("LinkedIn", o =>
               {
                   o.CorrelationCookie.HttpOnly = true;
                   o.CorrelationCookie.SameSite = SameSiteMode.Lax;

                   var linkedInSection = Configuration.GetSection("Authentication:LinkedIn");

                   o.ClientId = linkedInSection.GetSection("ClientId").Get<string>();
                   o.ClientSecret = linkedInSection.GetSection("ClientSecret").Get<string>();
                   o.CallbackPath = new PathString(linkedInSection.GetSection("CallbackPath").Get<string>());

                   o.AuthorizationEndpoint = linkedInSection.GetSection("AuthorizationEndpoint").Get<string>();
                   o.TokenEndpoint = linkedInSection.GetSection("TokenEndpoint").Get<string>();
                   o.UserInformationEndpoint = linkedInSection.GetSection("UserInformationEndpoint").Get<string>();

                   o.Scope.Add("r_liteprofile");
                   o.Scope.Add("r_emailaddress");

                   o.SaveTokens = true;
                   
                   o.Events = new OAuthEvents
                   {
                       OnCreatingTicket = async context =>
                       {
                           var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);

                           request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                           request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);

                           var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
                           response.EnsureSuccessStatusCode();

                           var payload = context.TokenResponse.Response;

                           var converter = new ExpandoObjectConverter();
                           dynamic user = JsonConvert.DeserializeObject<ExpandoObject>(await response.Content.ReadAsStringAsync(), converter);

                           var sourceid = user.id;

                           var firstName = user.firstName.localized.en_US;
                           var lastName = user.lastName.localized.en_US;
                           var profilePicture = user.profilePicture.displayImage;

                           var claims = new List<Claim>
                            {
                                new Claim(ClaimTypes.Name, sourceid),
                                new Claim("FirstName", firstName),
                                new Claim("LastName", lastName),
                                new Claim("ProfilePicture", profilePicture)

                            };

                           var claimsIdentity = new ClaimsIdentity(
                               claims, CookieAuthenticationDefaults.AuthenticationScheme);
                           context.Principal.AddIdentity(claimsIdentity);
                      }
4

0 回答 0