public async Task <ActionResult> Callback() { string redirectUrl = "http://localhost:4200/linkedin-auth.html"; try { var authCode = HttpContext.Request.Query["code"].ToString(); var state = HttpContext.Request.Query["state"].ToString(); var accessTokenResponse = await _oAuthService.GetAccessTokenAsync( authCode, "https://localhost:44364/api/externalauth/callback", new LinkedInOAuthSettings { ClientId = _linkedInAuthSettings.ClientId, ClientSecret = _linkedInAuthSettings.ClientSecret } ); if (accessTokenResponse != null && accessTokenResponse.AccessToken != null) { redirectUrl += $"?access_token={accessTokenResponse.AccessToken}"; } } catch (Exception ex) { _logger.LogError(ex, "Exception in ExternalAuthController:Callback action."); } return(Redirect(redirectUrl)); }
public async Task <IActionResult> GetAccessTokenAsync([FromQuery(Name = "request_token")] string requestToken) { var token = await _oauthService.GetAccessTokenAsync(requestToken); if (token == null) { return(Unauthorized("Unable to gain access token")); } var opts = new CookieOptions { //Secure = true, //HttpOnly = true, //IsEssential = true, }; Response.Cookies.Append("session", token, opts); return(NoContent()); }
public async Task Handle() { var apiUrl = _appSettings.ApiUrl; var accessToken = await _service.GetAccessTokenAsync(); var originalRequest = HttpContext.Request; var originalPath = HttpContext.Request.Path.ToString(); var prefix = @"/" + AppSettings.PassThroughPrefix; // value is const, must be gotten from class instead of instance var path = HttpContext.Request.Path.ToString().Remove(0, prefix.Length); var uri = apiUrl + path; var request = (HttpWebRequest)WebRequest.Create(uri + HttpContext.Request.QueryString); request.Method = HttpContext.Request.Method; request.ContentType = HttpContext.Request.ContentType; request.Headers["Authorization"] = "Bearer " + accessToken; //1000 * 60 * 10 request.ContinueTimeout = 600000; if (originalRequest.ContentLength.HasValue) { using (var requestBodyStream = originalRequest.Body) { byte[] requestBodyBytes; using (var br = new BinaryReader(requestBodyStream)) { requestBodyBytes = br.ReadBytes((int)originalRequest.ContentLength); } using (var requestStream = await request.GetRequestStreamAsync()) { await requestStream.WriteAsync(requestBodyBytes, 0, requestBodyBytes.Length); await requestStream.FlushAsync(); } } } try { using (var response = await request.GetResponseAsync()) { HttpContext.Response.ContentType = response.ContentType; HttpContext.Response.StatusCode = (int)((HttpWebResponse)response).StatusCode; if (response.Headers["x-filename"] != null) { HttpContext.Response.Headers["x-filename"] = response.Headers["x-filename"]; } if (response.Headers["content-disposition"] != null) { HttpContext.Response.Headers["content-disposition"] = response.Headers["content-disposition"]; } using (var responseStream = response.GetResponseStream()) { await responseStream.CopyToAsync(HttpContext.Response.Body); await HttpContext.Response.Body.FlushAsync(); } } } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ConnectFailure || ex.Status == WebExceptionStatus.UnknownError) { HttpContext.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable; return; } HttpContext.Response.ContentType = ((HttpWebResponse)ex.Response).ContentType; HttpContext.Response.StatusCode = (int)((HttpWebResponse)ex.Response).StatusCode; using (var responseStream = ex.Response.GetResponseStream()) { await responseStream.CopyToAsync(HttpContext.Response.Body); await HttpContext.Response.Body.FlushAsync(); } } }