public async Task <IActionResult> PcoLoginCallback(string code) { var redirectUrl = this.Url.Action("PcoLoginCallback", "Home", null, "https"); var client = new System.Net.Http.HttpClient(); var tokenRequest = new { grant_type = "authorization_code", code = code, client_id = _pcoApp.ClientID, client_secret = _pcoApp.ClientSecret, redirect_uri = redirectUrl }; var tokenRequestJson = Newtonsoft.Json.JsonConvert.SerializeObject(tokenRequest); var callbackResponse = await client.PostAsync(_pcoAuthOptions.AuthTokenUrl, new StringContent(tokenRequestJson, System.Text.Encoding.UTF8, "application/json")); var token = await callbackResponse.Content.ReadJsonAsync <PcoAuthTokenResponse>(); var pcoClient = new PcoApiClient.PcoApiClient(client, new PcoApiOptions() { AuthenticationMethod = "Bearer", Password = token.AccessToken }); var myInfo = await pcoClient.Get <PcoPeoplePerson>("people/v2/me"); var ident = new System.Security.Claims.ClaimsIdentity("PCO"); ident.AddClaim(new Claim(ClaimTypes.NameIdentifier, myInfo.Data.ID)); ident.AddClaim(new Claim(ClaimTypes.Name, myInfo.Data.Attributes.Name)); ident.AddClaim(new Claim(ClaimsExtensions.OrganizationID, myInfo.Meta.Parent.ID.ToString())); ident.AddClaim(new Claim(ClaimsExtensions.AccessToken, token.AccessToken)); ident.AddClaim(new Claim(ClaimsExtensions.RefreshToken, token.RefreshToken)); var principal = new System.Security.Claims.ClaimsPrincipal(ident); await this.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties() { ExpiresUtc = DateTimeOffset.UtcNow.AddHours(1), AllowRefresh = true, IsPersistent = true }); return(RedirectToAction("Index")); }
public PcoHelper(PcoApiClient.PcoApiClient pcoClient, PcoTenant tenant, IMemoryCache cache) { _pcoClient = pcoClient; _tenant = tenant; _cache = cache; }