Skip to content

Commit 4fcb064

Browse files
committed
Update aspnet core sample with latest
1 parent 48ccb86 commit 4fcb064

Some content is hidden

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

51 files changed

+23933
-127
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
{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<Version>3.0.0</Version>
6+
<Company>Microsoft</Company>
7+
<Product>Microsoft Graph</Product>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.1.5" />
12+
<PackageReference Include="Microsoft.Graph" Version="3.8.0" />
13+
<PackageReference Include="Microsoft.Identity.Client" Version="4.16.1" />
14+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
15+
</ItemGroup>
16+
17+
</Project>
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.30002.166
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27004.2005
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mgt-netcore", "mgt-netcore.csproj", "{303A09F7-68A3-4010-B802-FB2A96A491F4}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MicrosoftGraphAspNetCoreConnectSample", "MicrosoftGraphAspNetCoreConnectSample.csproj", "{8CFC9CD3-FFB9-4EED-8734-4678333D41C3}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1010
Debug|Any CPU = Debug|Any CPU
1111
Release|Any CPU = Release|Any CPU
1212
EndGlobalSection
1313
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14-
{303A09F7-68A3-4010-B802-FB2A96A491F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15-
{303A09F7-68A3-4010-B802-FB2A96A491F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16-
{303A09F7-68A3-4010-B802-FB2A96A491F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17-
{303A09F7-68A3-4010-B802-FB2A96A491F4}.Release|Any CPU.Build.0 = Release|Any CPU
14+
{8CFC9CD3-FFB9-4EED-8734-4678333D41C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{8CFC9CD3-FFB9-4EED-8734-4678333D41C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{8CFC9CD3-FFB9-4EED-8734-4678333D41C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{8CFC9CD3-FFB9-4EED-8734-4678333D41C3}.Release|Any CPU.Build.0 = Release|Any CPU
1818
EndGlobalSection
1919
GlobalSection(SolutionProperties) = preSolution
2020
HideSolutionNode = FALSE
2121
EndGlobalSection
2222
GlobalSection(ExtensibilityGlobals) = postSolution
23-
SolutionGuid = {1209990C-61E8-46B0-B0C0-D96BF4B961F1}
23+
SolutionGuid = {7C99A97D-FC2E-4D17-AD3B-46460F0EF371}
2424
EndGlobalSection
2525
EndGlobal

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"iisSettings": {
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,

samples/proxy-provider-asp-net-core/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
To use the Microsoft Graph Toolkit with backend authentication, one solution is to proxy all calls from the front end to the back end. The Microsoft Graph Toolkit ships with an authentication provider implementation (ProxyProvider) that enables all components to call the Microsoft Graph via the backend.
44

5-
This sample is a reference on how to leverage the [ProxyProvider](https://docs.microsoft.com/graph/toolkit/providers/proxy) with an ASP.NET Core backend. However, it is worth nothing that the ProxyProvider can work with any backend service.
5+
This sample is a reference on how to leverage the [ProxyProvider](https://docs.microsoft.com/graph/toolkit/providers/proxy) with an ASP.NET Core backend. However, it is worth noting that the ProxyProvider can work with any backend service.
66

77
## Client code
88

samples/proxy-provider-asp-net-core/Helpers/GraphAuthProvider.cs renamed to samples/proxy-provider-asp-net-core/Services/GraphAuthProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
using Microsoft.Graph;
1111
using MicrosoftGraphAspNetCoreConnectSample.Extensions;
1212

13-
namespace MicrosoftGraphAspNetCoreConnectSample.Helpers
13+
namespace MicrosoftGraphAspNetCoreConnectSample.Services
1414
{
1515
public class GraphAuthProvider : IGraphAuthProvider
1616
{
17-
private IConfidentialClientApplication _app;
17+
private readonly IConfidentialClientApplication _app;
1818
private readonly string[] _scopes;
1919

2020
public GraphAuthProvider(IConfiguration configuration)

0 commit comments

Comments
 (0)