Skip to content

Commit 1f57dc2

Browse files
authored
Damienbod/update 2-WebApp-graph-user\2-1-Call-MSGraph (#401)
* updating bootstrap 4, using latest templates * updating to latest css * styling * update version * Updated C# code to match the templates * Added Token Cache help info back * removing consent, no longer part of the templates * small clean up * removing a space * switching back to TargetFramework
1 parent ee35fff commit 1f57dc2

File tree

76 files changed

+40216
-24398
lines changed

Some content is hidden

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

76 files changed

+40216
-24398
lines changed
Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1-
2-
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29123.89
5-
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApp-OpenIDConnect-DotNet", "WebApp-OpenIDConnect-DotNet.csproj", "{8DCFEEC2-0A85-4C7E-B96A-21C9184470B1}"
7-
EndProject
8-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{CD0BB564-6C1E-4FCE-B9AB-7C637FDEE569}"
9-
ProjectSection(SolutionItems) = preProject
10-
.editorconfig = .editorconfig
11-
EndProjectSection
12-
EndProject
13-
Global
14-
GlobalSection(SolutionConfigurationPlatforms) = preSolution
15-
Debug|Any CPU = Debug|Any CPU
16-
Release|Any CPU = Release|Any CPU
17-
EndGlobalSection
18-
GlobalSection(ProjectConfigurationPlatforms) = postSolution
19-
{8DCFEEC2-0A85-4C7E-B96A-21C9184470B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20-
{8DCFEEC2-0A85-4C7E-B96A-21C9184470B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
21-
{8DCFEEC2-0A85-4C7E-B96A-21C9184470B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
22-
{8DCFEEC2-0A85-4C7E-B96A-21C9184470B1}.Release|Any CPU.Build.0 = Release|Any CPU
23-
EndGlobalSection
24-
GlobalSection(SolutionProperties) = preSolution
25-
HideSolutionNode = FALSE
26-
EndGlobalSection
27-
GlobalSection(ExtensibilityGlobals) = postSolution
28-
SolutionGuid = {4FC144FA-5678-428C-B5B7-E2A88ADE849D}
29-
EndGlobalSection
30-
EndGlobal
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30413.136
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApp-OpenIDConnect-DotNet", "WebApp-OpenIDConnect-DotNet.csproj", "{76F9C1E5-3CF7-4C9A-A0EC-D15B5F11022D}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{76F9C1E5-3CF7-4C9A-A0EC-D15B5F11022D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{76F9C1E5-3CF7-4C9A-A0EC-D15B5F11022D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{76F9C1E5-3CF7-4C9A-A0EC-D15B5F11022D}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{76F9C1E5-3CF7-4C9A-A0EC-D15B5F11022D}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {AB49E34F-5B62-4F54-BAEA-C6473483C761}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,76 @@
1-
using System;
2-
using System.Diagnostics;
3-
using System.IO;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Authorization;
6-
using Microsoft.AspNetCore.Mvc;
7-
using Microsoft.Extensions.Options;
8-
using Graph=Microsoft.Graph;
9-
using Microsoft.Identity.Web;
10-
using WebApp_OpenIDConnect_DotNet.Infrastructure;
11-
using WebApp_OpenIDConnect_DotNet.Models;
12-
using WebApp_OpenIDConnect_DotNet.Services;
13-
14-
namespace WebApp_OpenIDConnect_DotNet.Controllers
15-
{
16-
[Authorize]
17-
public class HomeController : Controller
18-
{
19-
readonly ITokenAcquisition tokenAcquisition;
20-
readonly WebOptions webOptions;
21-
22-
public HomeController(ITokenAcquisition tokenAcquisition,
23-
IOptions<WebOptions> webOptionValue)
24-
{
25-
this.tokenAcquisition = tokenAcquisition;
26-
this.webOptions = webOptionValue.Value;
27-
}
28-
29-
public IActionResult Index()
30-
{
31-
return View();
32-
}
33-
34-
[AuthorizeForScopes(Scopes = new[] { Constants.ScopeUserRead })]
35-
public async Task<IActionResult> Profile()
36-
{
37-
// Initialize the GraphServiceClient.
38-
Graph::GraphServiceClient graphClient = GetGraphServiceClient(new[] { Constants.ScopeUserRead });
39-
40-
var me = await graphClient.Me.Request().GetAsync();
41-
ViewData["Me"] = me;
42-
43-
try
44-
{
45-
// Get user photo
46-
using (var photoStream = await graphClient.Me.Photo.Content.Request().GetAsync())
47-
{
48-
byte[] photoByte = ((MemoryStream)photoStream).ToArray();
49-
ViewData["Photo"] = Convert.ToBase64String(photoByte);
50-
}
51-
}
52-
catch (System.Exception)
53-
{
54-
ViewData["Photo"] = null;
55-
}
56-
57-
return View();
58-
}
59-
60-
private Graph::GraphServiceClient GetGraphServiceClient(string[] scopes)
61-
{
62-
return GraphServiceClientFactory.GetAuthenticatedGraphClient(async () =>
63-
{
64-
string result = await tokenAcquisition.GetAccessTokenForUserAsync(scopes);
65-
return result;
66-
}, webOptions.GraphApiUrl);
67-
}
68-
69-
[AllowAnonymous]
70-
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
71-
public IActionResult Error()
72-
{
73-
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
74-
}
75-
}
76-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Authorization;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Identity.Web;
9+
using System.Net;
10+
using System.Net.Http;
11+
using Microsoft.Graph;
12+
using Microsoft.AspNetCore.Mvc;
13+
using Microsoft.Extensions.Logging;
14+
using _2_1_Call_MSGraph.Models;
15+
using System.IO;
16+
17+
namespace _2_1_Call_MSGraph.Controllers
18+
{
19+
[Authorize]
20+
public class HomeController : Controller
21+
{
22+
private readonly ILogger<HomeController> _logger;
23+
24+
private readonly GraphServiceClient _graphServiceClient;
25+
26+
public HomeController(ILogger<HomeController> logger,
27+
GraphServiceClient graphServiceClient)
28+
{
29+
_logger = logger;
30+
_graphServiceClient = graphServiceClient;
31+
}
32+
33+
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
34+
public async Task<IActionResult> Index()
35+
{
36+
var user = await _graphServiceClient.Me.Request().GetAsync();
37+
ViewData["ApiResult"] = user.DisplayName;
38+
39+
return View();
40+
}
41+
42+
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
43+
public async Task<IActionResult> Profile()
44+
{
45+
var me = await _graphServiceClient.Me.Request().GetAsync();
46+
ViewData["Me"] = me;
47+
48+
try
49+
{
50+
// Get user photo
51+
using (var photoStream = await _graphServiceClient.Me.Photo.Content.Request().GetAsync())
52+
{
53+
byte[] photoByte = ((MemoryStream)photoStream).ToArray();
54+
ViewData["Photo"] = Convert.ToBase64String(photoByte);
55+
}
56+
}
57+
catch (System.Exception)
58+
{
59+
ViewData["Photo"] = null;
60+
}
61+
62+
return View();
63+
}
64+
public IActionResult Privacy()
65+
{
66+
return View();
67+
}
68+
69+
[AllowAnonymous]
70+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
71+
public IActionResult Error()
72+
{
73+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
74+
}
75+
}
76+
}

2-WebApp-graph-user/2-1-Call-MSGraph/Infrastructure/Constants.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
namespace WebApp_OpenIDConnect_DotNet.Models
2-
{
3-
public class ErrorViewModel
4-
{
5-
public string RequestId { get; set; }
6-
7-
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8-
}
9-
}
1+
using System;
2+
3+
namespace _2_1_Call_MSGraph.Models
4+
{
5+
public class ErrorViewModel
6+
{
7+
public string RequestId { get; set; }
8+
9+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10+
}
11+
}
Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
using Microsoft.Extensions.Hosting;
2-
using Microsoft.AspNetCore.Hosting;
3-
4-
namespace WebApp_OpenIDConnect_DotNet
5-
{
6-
public class Program
7-
{
8-
public static void Main(string[] args)
9-
{
10-
CreateHostBuilder(args).Build().Run();
11-
}
12-
13-
public static IHostBuilder CreateHostBuilder(string[] args) =>
14-
Host.CreateDefaultBuilder(args)
15-
.ConfigureWebHostDefaults(webBuilder =>
16-
{
17-
webBuilder.UseStartup<Startup>();
18-
});
19-
}
20-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace _2_1_Call_MSGraph
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}

2-WebApp-graph-user/2-1-Call-MSGraph/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ services: active-directory
33
platforms: dotnet
44
author: jmprieur
55
level: 200
6-
client: ASP.NET Core 2.x Web App
6+
client: ASP.NET Core 3.x Web App
77
service: Microsoft Graph
88
endpoint: Microsoft identity platform
99
---

2-WebApp-graph-user/2-1-Call-MSGraph/Services/Bootstrapper.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

2-WebApp-graph-user/2-1-Call-MSGraph/Services/GraphServiceClientFactory.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

2-WebApp-graph-user/2-1-Call-MSGraph/Services/WebOptions.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)