-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a sample using the Singleton Token acquisition and the GraphSe…
…rviceClient (#159) * Adding a sample using the Singleton Token acquisition and the GraphServiceClient * some suggestions and add license info (#160) * Adressing most PR comments - GraphServiceClient instead of IGraphServiceClient per Graph SDK guidance (@darrelmiller) - Adding the handling of the incremental consent in the Razor model - Moving the default scopes to AddMicrosoftGraph and adding a setting to express the Graph base URL (for national clouds for instance) - Renaming the token credential provider Co-authored-by: jennyf19 <jeferrie@microsoft.com>
- Loading branch information
Showing
57 changed files
with
39,834 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
tests/WebAppCallsMicrosoftGraph/MicrosoftGraphServiceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Graph; | ||
using Microsoft.Identity.Web; | ||
|
||
namespace WebAppCallsMicrosoftGraph | ||
{ | ||
public static class MicrosoftGraphServiceExtensions | ||
{ | ||
/// <summary> | ||
/// Adds the Microsoft Graph client as a singleton. | ||
/// </summary> | ||
/// <param name="services">Service collection.</param> | ||
/// <param name="configuration">Configuration for Microsoft Graph.</param> | ||
/// <param name="initialScopes">Initial scopes.</param> | ||
/// <param name="graphBaseUrlKey">Base URL for Microsoft graph. This can be | ||
/// changed for instance for applications running in national clouds</param> | ||
public static void AddMicrosoftGraph(this IServiceCollection services, | ||
IConfiguration configuration, | ||
IEnumerable<string> initialScopes, | ||
string graphBaseUrlKey = "MicrosoftGraphBaseUrl") | ||
{ | ||
// Graph base URL | ||
string graphBaseUrl = configuration.GetValue<string>(graphBaseUrlKey); | ||
|
||
|
||
services.AddSingleton<GraphServiceClient, GraphServiceClient>(serviceProvider => | ||
{ | ||
var tokenAquisitionService = serviceProvider.GetService<ITokenAcquisition>(); | ||
GraphServiceClient client = string.IsNullOrWhiteSpace(graphBaseUrl) ? | ||
new GraphServiceClient(new TokenAcquisitionCredentialProvider(tokenAquisitionService, initialScopes)) : | ||
new GraphServiceClient(graphBaseUrl, new TokenAcquisitionCredentialProvider(tokenAquisitionService, initialScopes)); | ||
return client; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@page | ||
@model ErrorModel | ||
@{ | ||
ViewData["Title"] = "Error"; | ||
} | ||
|
||
<h1 class="text-danger">Error.</h1> | ||
<h2 class="text-danger">An error occurred while processing your request.</h2> | ||
|
||
@if (Model.ShowRequestId) | ||
{ | ||
<p> | ||
<strong>Request ID:</strong> <code>@Model.RequestId</code> | ||
</p> | ||
} | ||
|
||
<h3>Development Mode</h3> | ||
<p> | ||
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred. | ||
</p> | ||
<p> | ||
<strong>The Development environment shouldn't be enabled for deployed applications.</strong> | ||
It can result in displaying sensitive information from exceptions to end users. | ||
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> | ||
and restarting the app. | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Diagnostics; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WebAppCallsMicrosoftGraph.Pages | ||
{ | ||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | ||
public class ErrorModel : PageModel | ||
{ | ||
public string RequestId { get; set; } | ||
|
||
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
|
||
private readonly ILogger<ErrorModel> _logger; | ||
|
||
public ErrorModel(ILogger<ErrorModel> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public void OnGet() | ||
{ | ||
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
@page | ||
@model IndexModel | ||
@{ | ||
ViewData["Title"] = "Home page"; | ||
string name = ViewData["name"] as string; | ||
} | ||
|
||
<div class="text-center"> | ||
<h1 class="display-4">Welcome</h1> | ||
<p>@name, Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> | ||
|
||
<table class="table table-striped table-condensed" style="font-family: monospace"> | ||
<tr> | ||
<th>Property</th> | ||
<th>Value</th> | ||
</tr> | ||
<tr> | ||
<td>photo</td> | ||
<td> | ||
@{ | ||
if (ViewData["photo"] != null) | ||
{ | ||
<img style="margin: 5px 0; width: 150px" src="data:image/jpeg;base64, @ViewData["photo"]" /> | ||
} | ||
else | ||
{ | ||
<h3>NO PHOTO</h3> | ||
<p>Check user profile in Azure Active Directory to add a photo.</p> | ||
} | ||
} | ||
</td> | ||
</tr> | ||
</table> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Graph; | ||
using Microsoft.Identity.Web; | ||
|
||
namespace WebAppCallsMicrosoftGraph.Pages | ||
{ | ||
[AuthorizeForScopes(Scopes = new[] { "user.read" })] | ||
public class IndexModel : PageModel | ||
{ | ||
private readonly ILogger<IndexModel> _logger; | ||
private readonly GraphServiceClient _graphServiceClient; | ||
|
||
public IndexModel(ILogger<IndexModel> logger, GraphServiceClient graphServiceClient) | ||
{ | ||
_logger = logger; | ||
_graphServiceClient = graphServiceClient; | ||
} | ||
|
||
public async Task OnGet() | ||
{ | ||
var user = await _graphServiceClient.Me.Request().GetAsync(); | ||
|
||
try | ||
{ | ||
using (var photoStream = await _graphServiceClient.Me.Photo.Content.Request().GetAsync()) | ||
{ | ||
byte[] photoByte = ((MemoryStream)photoStream).ToArray(); | ||
ViewData["photo"] = Convert.ToBase64String(photoByte); | ||
} | ||
ViewData["name"] = user.DisplayName; | ||
} | ||
catch (Exception) | ||
{ | ||
ViewData["photo"] = null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@page | ||
@model PrivacyModel | ||
@{ | ||
ViewData["Title"] = "Privacy Policy"; | ||
} | ||
<h1>@ViewData["Title"]</h1> | ||
|
||
<p>Use this page to detail your site's privacy policy.</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WebAppCallsMicrosoftGraph.Pages | ||
{ | ||
public class PrivacyModel : PageModel | ||
{ | ||
private readonly ILogger<PrivacyModel> _logger; | ||
|
||
public PrivacyModel(ILogger<PrivacyModel> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public void OnGet() | ||
{ | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
tests/WebAppCallsMicrosoftGraph/Pages/Shared/_Layout.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>@ViewData["Title"] - WebAppCallsMicrosoftGraph</title> | ||
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" /> | ||
<link rel="stylesheet" href="~/css/site.css" /> | ||
</head> | ||
<body> | ||
<header> | ||
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"> | ||
<div class="container"> | ||
<a class="navbar-brand" asp-area="" asp-page="/Index">WebAppCallsMicrosoftGraph</a> | ||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent" | ||
aria-expanded="false" aria-label="Toggle navigation"> | ||
<span class="navbar-toggler-icon"></span> | ||
</button> | ||
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse"> | ||
<partial name="_LoginPartial" /> | ||
<ul class="navbar-nav flex-grow-1"> | ||
<li class="nav-item"> | ||
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a> | ||
</li> | ||
<li class="nav-item"> | ||
<a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a> | ||
</li> | ||
</ul> | ||
</div> | ||
</div> | ||
</nav> | ||
</header> | ||
<div class="container"> | ||
<main role="main" class="pb-3"> | ||
@RenderBody() | ||
</main> | ||
</div> | ||
|
||
<footer class="border-top footer text-muted"> | ||
<div class="container"> | ||
© 2020 - WebAppCallsMicrosoftGraph - <a asp-area="" asp-page="/Privacy">Privacy</a> | ||
</div> | ||
</footer> | ||
|
||
<script src="~/lib/jquery/dist/jquery.min.js"></script> | ||
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> | ||
<script src="~/js/site.js" asp-append-version="true"></script> | ||
|
||
@await RenderSectionAsync("Scripts", required: false) | ||
</body> | ||
</html> |
18 changes: 18 additions & 0 deletions
18
tests/WebAppCallsMicrosoftGraph/Pages/Shared/_LoginPartial.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
<ul class="navbar-nav"> | ||
@if (User.Identity.IsAuthenticated) | ||
{ | ||
<li class="nav-item"> | ||
<span class="navbar-text text-dark">Hello @User.Identity.Name!</span> | ||
</li> | ||
<li class="nav-item"> | ||
<a class="nav-link text-dark" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignOut">Sign out</a> | ||
</li> | ||
} | ||
else | ||
{ | ||
<li class="nav-item"> | ||
<a class="nav-link text-dark" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignIn">Sign in</a> | ||
</li> | ||
} | ||
</ul> |
2 changes: 2 additions & 0 deletions
2
tests/WebAppCallsMicrosoftGraph/Pages/Shared/_ValidationScriptsPartial.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script> | ||
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@using WebAppCallsMicrosoftGraph | ||
@namespace WebAppCallsMicrosoftGraph.Pages | ||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@{ | ||
Layout = "_Layout"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace WebAppCallsMicrosoftGraph | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/WebAppCallsMicrosoftGraph/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:23037", | ||
"sslPort": 44395 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"WebAppCallsMicrosoftGraph": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"applicationUrl": "https://localhost:5001;http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.