Skip to content

Commit 97b7cfa

Browse files
authored
Merge pull request #568 from microsoftgraph/bethpan/aspnetcore-sample
Update aspnet core sample with latest
2 parents 48ccb86 + ca51dbf commit 97b7cfa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+24013
-144
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"directory": "wwwroot/lib"
3+
}

samples/proxy-provider-asp-net-core/Controllers/HomeController.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,22 @@
66
using System.Threading.Tasks;
77
using Microsoft.AspNetCore.Authorization;
88
using Microsoft.AspNetCore.Mvc;
9-
using MicrosoftGraphAspNetCoreConnectSample.Helpers;
10-
using Microsoft.Extensions.Configuration;
119
using Microsoft.Graph;
1210
using Microsoft.AspNetCore.Hosting;
1311
using System.Security.Claims;
12+
using MicrosoftGraphAspNetCoreConnectSample.Services;
1413

1514
namespace MicrosoftGraphAspNetCoreConnectSample.Controllers
1615
{
1716
public class HomeController : Controller
1817
{
19-
private readonly IConfiguration _configuration;
20-
private readonly IHostingEnvironment _env;
21-
private readonly IGraphSdkHelper _graphSdkHelper;
18+
private readonly IWebHostEnvironment _env;
19+
private readonly IGraphServiceClientFactory _graphServiceClientFactory;
2220

23-
public HomeController(IConfiguration configuration, IHostingEnvironment hostingEnvironment, IGraphSdkHelper graphSdkHelper)
21+
public HomeController(IWebHostEnvironment hostingEnvironment, IGraphServiceClientFactory graphServiceClientFactory)
2422
{
25-
_configuration = configuration;
2623
_env = hostingEnvironment;
27-
_graphSdkHelper = graphSdkHelper;
24+
_graphServiceClientFactory = graphServiceClientFactory;
2825
}
2926

3027
[AllowAnonymous]
@@ -34,11 +31,11 @@ public async Task<IActionResult> Index(string email)
3431
if (User.Identity.IsAuthenticated)
3532
{
3633
// Get users's email.
37-
email = email ?? User.FindFirst("preferred_username")?.Value;
34+
email ??= User.FindFirst("preferred_username")?.Value;
3835
ViewData["Email"] = email;
3936

4037
// Initialize the GraphServiceClient.
41-
var graphClient = _graphSdkHelper.GetAuthenticatedClient((ClaimsIdentity)User.Identity);
38+
var graphClient = _graphServiceClientFactory.GetAuthenticatedGraphClient((ClaimsIdentity)User.Identity);
4239

4340
ViewData["Response"] = await GraphService.GetUserJson(graphClient, email, HttpContext);
4441

@@ -62,7 +59,7 @@ public async Task<IActionResult> SendEmail(string recipients)
6259
try
6360
{
6461
// Initialize the GraphServiceClient.
65-
var graphClient = _graphSdkHelper.GetAuthenticatedClient((ClaimsIdentity)User.Identity);
62+
var graphClient = _graphServiceClientFactory.GetAuthenticatedGraphClient((ClaimsIdentity)User.Identity);
6663

6764
// Send the email.
6865
await GraphService.SendEmail(graphClient, _env, recipients, HttpContext);
@@ -78,10 +75,16 @@ public async Task<IActionResult> SendEmail(string recipients)
7875
}
7976
}
8077

78+
[AllowAnonymous]
8179
public IActionResult Messages()
8280
{
8381
return View();
8482
}
83+
84+
public IActionResult Privacy()
85+
{
86+
return View();
87+
}
8588

8689
[AllowAnonymous]
8790
public IActionResult Error()

samples/proxy-provider-asp-net-core/Controllers/ProxyController.cs

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Net;
34
using System.Net.Http;
45
using System.Net.Http.Headers;
@@ -9,60 +10,63 @@
910
using Microsoft.AspNetCore.Http;
1011
using Microsoft.AspNetCore.Mvc;
1112
using Microsoft.Extensions.Configuration;
13+
using Microsoft.Extensions.Primitives;
1214
using Microsoft.Graph;
13-
using MicrosoftGraphAspNetCoreConnectSample.Helpers;
15+
using MicrosoftGraphAspNetCoreConnectSample.Services;
1416

1517
namespace 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+
}

samples/proxy-provider-asp-net-core/Extensions/AzureAdAuthenticationBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Microsoft.Extensions.Options;
77
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
88
using Microsoft.IdentityModel.Tokens;
9-
using MicrosoftGraphAspNetCoreConnectSample.Helpers;
9+
using MicrosoftGraphAspNetCoreConnectSample.Services;
1010

1111
namespace MicrosoftGraphAspNetCoreConnectSample.Extensions
1212
{

samples/proxy-provider-asp-net-core/Program.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
/*
1+
/*
22
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
33
* See LICENSE in the source repository root for complete license information.
44
*/
55

66
using Microsoft.AspNetCore;
77
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Logging;
89

910
namespace MicrosoftGraphAspNetCoreConnectSample
1011
{
@@ -17,6 +18,11 @@ public static void Main(string[] args)
1718

1819
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
1920
WebHost.CreateDefaultBuilder(args)
21+
.ConfigureLogging((hostingContext, logging) =>
22+
{
23+
logging.AddConsole();
24+
logging.AddDebug();
25+
})
2026
.UseStartup<Startup>();
2127
}
2228
}

samples/proxy-provider-asp-net-core/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"iisSettings": {
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
@@ -22,7 +22,7 @@
2222
"environmentVariables": {
2323
"ASPNETCORE_ENVIRONMENT": "Development"
2424
},
25-
"applicationUrl": "http://localhost:60761"
25+
"applicationUrl": "http://localhost:44334"
2626
}
2727
}
2828
}

0 commit comments

Comments
 (0)