1- using System . Linq ;
1+ using System . Collections . Generic ;
2+ using System . Linq ;
23using System . Net ;
34using System . Net . Http ;
45using System . Net . Http . Headers ;
910using Microsoft . AspNetCore . Http ;
1011using Microsoft . AspNetCore . Mvc ;
1112using Microsoft . Extensions . Configuration ;
13+ using Microsoft . Extensions . Primitives ;
1214using Microsoft . Graph ;
13- using MicrosoftGraphAspNetCoreConnectSample . Helpers ;
15+ using MicrosoftGraphAspNetCoreConnectSample . Services ;
1416
1517namespace MicrosoftGraphAspNetCoreConnectSample . Controllers
1618{
1719 [ Route ( "api/[controller]" ) ]
1820 [ ApiController ]
1921 public class ProxyController : ControllerBase
2022 {
21- private readonly IGraphSdkHelper _graphSdkHelper ;
23+ private readonly IWebHostEnvironment _env ;
24+ private readonly IGraphServiceClientFactory _graphServiceClientFactory ;
2225
23- public ProxyController ( IConfiguration configuration , IHostingEnvironment hostingEnvironment , IGraphSdkHelper graphSdkHelper )
26+ public ProxyController ( IWebHostEnvironment hostingEnvironment , IGraphServiceClientFactory graphServiceClientFactory )
2427 {
25- _graphSdkHelper = graphSdkHelper ;
28+ _env = hostingEnvironment ;
29+ _graphServiceClientFactory = graphServiceClientFactory ;
2630 }
2731
2832 [ HttpGet ]
2933 [ Route ( "{*all}" ) ]
30- public async Task < HttpResponseMessage > GetAsync ( string all )
34+ public async Task < IActionResult > GetAsync ( string all )
3135 {
3236 return await ProcessRequestAsync ( "GET" , all , null ) . ConfigureAwait ( false ) ;
3337 }
3438
3539 [ HttpPost ]
3640 [ Route ( "{*all}" ) ]
37- public async Task < HttpResponseMessage > PostAsync ( string all , [ FromBody ] object body )
41+ public async Task < IActionResult > PostAsync ( string all , [ FromBody ] object body )
3842 {
3943 return await ProcessRequestAsync ( "POST" , all , body ) . ConfigureAwait ( false ) ;
4044 }
4145
4246 [ HttpDelete ]
4347 [ Route ( "{*all}" ) ]
44- public async Task < HttpResponseMessage > DeleteAsync ( string all )
48+ public async Task < IActionResult > DeleteAsync ( string all )
4549 {
4650 return await ProcessRequestAsync ( "DELETE" , all , null ) . ConfigureAwait ( false ) ;
4751 }
4852
4953 [ HttpPut ]
5054 [ Route ( "{*all}" ) ]
51- public async Task < HttpResponseMessage > PutAsync ( string all , [ FromBody ] object body )
55+ public async Task < IActionResult > PutAsync ( string all , [ FromBody ] object body )
5256 {
5357 return await ProcessRequestAsync ( "PUT" , all , body ) . ConfigureAwait ( false ) ;
5458 }
5559
5660 [ HttpPatch ]
5761 [ Route ( "{*all}" ) ]
58- public async Task < HttpResponseMessage > PatchAsync ( string all , [ FromBody ] object body )
62+ public async Task < IActionResult > PatchAsync ( string all , [ FromBody ] object body )
5963 {
6064 return await ProcessRequestAsync ( "PATCH" , all , body ) . ConfigureAwait ( false ) ;
6165 }
6266
63- private async Task < HttpResponseMessage > ProcessRequestAsync ( string method , string all , object content )
67+ private async Task < IActionResult > ProcessRequestAsync ( string method , string all , object content )
6468 {
65- var graphClient = _graphSdkHelper . GetAuthenticatedClient ( ( ClaimsIdentity ) User . Identity ) ;
69+ var graphClient = _graphServiceClientFactory . GetAuthenticatedGraphClient ( ( ClaimsIdentity ) User . Identity ) ;
6670
6771 var qs = HttpContext . Request . QueryString ;
6872 var url = $ "{ GetBaseUrlWithoutVersion ( graphClient ) } /{ all } { qs . ToUriComponent ( ) } ";
@@ -93,12 +97,12 @@ private async Task<HttpResponseMessage> ProcessRequestAsync(string method, strin
9397 contentType = contentTypes ? . FirstOrDefault ( ) ?? contentType ;
9498
9599 var byteArrayContent = await response . Content . ReadAsByteArrayAsync ( ) . ConfigureAwait ( false ) ;
96- return ReturnHttpResponseMessage ( HttpStatusCode . OK , contentType , new ByteArrayContent ( byteArrayContent ) ) ;
100+ return new HttpResponseMessageResult ( ReturnHttpResponseMessage ( HttpStatusCode . OK , contentType , new ByteArrayContent ( byteArrayContent ) ) ) ;
97101 }
98102 }
99103 catch ( ServiceException ex )
100104 {
101- return ReturnHttpResponseMessage ( ex . StatusCode , contentType , new StringContent ( ex . Error . ToString ( ) ) ) ;
105+ return new HttpResponseMessageResult ( ReturnHttpResponseMessage ( ex . StatusCode , contentType , new StringContent ( ex . Error . ToString ( ) ) ) ) ;
102106 }
103107 }
104108
@@ -127,5 +131,34 @@ private string GetBaseUrlWithoutVersion(GraphServiceClient graphClient)
127131 var index = baseUrl . LastIndexOf ( '/' ) ;
128132 return baseUrl . Substring ( 0 , index ) ;
129133 }
134+
135+ public class HttpResponseMessageResult : IActionResult
136+ {
137+ private readonly HttpResponseMessage _responseMessage ;
138+
139+ public HttpResponseMessageResult ( HttpResponseMessage responseMessage )
140+ {
141+ _responseMessage = responseMessage ; // could add throw if null
142+ }
143+
144+ public async Task ExecuteResultAsync ( ActionContext context )
145+ {
146+ context . HttpContext . Response . StatusCode = ( int ) _responseMessage . StatusCode ;
147+
148+ foreach ( var header in _responseMessage . Headers )
149+ {
150+ context . HttpContext . Response . Headers . TryAdd ( header . Key , new StringValues ( header . Value . ToArray ( ) ) ) ;
151+ }
152+
153+ context . HttpContext . Response . ContentType = _responseMessage . Content . Headers . ContentType . ToString ( ) ;
154+
155+ using ( var stream = await _responseMessage . Content . ReadAsStreamAsync ( ) )
156+ {
157+ await stream . CopyToAsync ( context . HttpContext . Response . Body ) ;
158+ await context . HttpContext . Response . Body . FlushAsync ( ) ;
159+ }
160+ }
161+ }
162+
130163 }
131- }
164+ }
0 commit comments