-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Calls Graph support to Blazor WASM config (#1851)
* Add Calls Graph support to Blazor WASM config * Remove downstreamApi * Store codefiles as .txt
- Loading branch information
Showing
15 changed files
with
915 additions
and
592 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
94 changes: 94 additions & 0 deletions
94
...oft.DotNet.MSIdentity/CodeReaderWriter/CodeFiles/Blazor/wasm/GraphClientExtensions.cs.txt
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,94 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Components.WebAssembly.Authentication; | ||
using Microsoft.Authentication.WebAssembly.Msal.Models; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Graph; | ||
|
||
/// <summary> | ||
/// Adds services and implements methods to use Microsoft Graph SDK. | ||
/// </summary> | ||
internal static class GraphClientExtensions | ||
{ | ||
/// <summary> | ||
/// Extension method for adding the Microsoft Graph SDK to IServiceCollection. | ||
/// </summary> | ||
/// <param name="services"></param> | ||
/// <param name="scopes">The MS Graph scopes to request</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddMicrosoftGraphClient(this IServiceCollection services, params string[] scopes) | ||
{ | ||
services.Configure<RemoteAuthenticationOptions<MsalProviderOptions>>(options => | ||
{ | ||
foreach (var scope in scopes) | ||
{ | ||
options.ProviderOptions.AdditionalScopesToConsent.Add(scope); | ||
} | ||
}); | ||
|
||
services.AddScoped<IAuthenticationProvider, GraphAuthenticationProvider>(); | ||
services.AddScoped<IHttpProvider, HttpClientHttpProvider>(sp => new HttpClientHttpProvider(new HttpClient())); | ||
services.AddScoped(sp => new GraphServiceClient( | ||
sp.GetRequiredService<IAuthenticationProvider>(), | ||
sp.GetRequiredService<IHttpProvider>())); | ||
return services; | ||
} | ||
|
||
/// <summary> | ||
/// Implements IAuthenticationProvider interface. | ||
/// Tries to get an access token for Microsoft Graph. | ||
/// </summary> | ||
private class GraphAuthenticationProvider : IAuthenticationProvider | ||
{ | ||
public GraphAuthenticationProvider(IAccessTokenProvider provider) | ||
{ | ||
Provider = provider; | ||
} | ||
|
||
public IAccessTokenProvider Provider { get; } | ||
|
||
public async Task AuthenticateRequestAsync(HttpRequestMessage request) | ||
{ | ||
var result = await Provider.RequestAccessToken(new AccessTokenRequestOptions() | ||
{ | ||
Scopes = new[] { "https://graph.microsoft.com/User.Read" } | ||
}); | ||
|
||
if (result.TryGetToken(out var token)) | ||
{ | ||
request.Headers.Authorization ??= new AuthenticationHeaderValue("Bearer", token.Value); | ||
} | ||
} | ||
} | ||
|
||
private class HttpClientHttpProvider : IHttpProvider | ||
{ | ||
private readonly HttpClient _client; | ||
|
||
public HttpClientHttpProvider(HttpClient client) | ||
{ | ||
_client = client; | ||
} | ||
|
||
public ISerializer Serializer { get; } = new Serializer(); | ||
|
||
public TimeSpan OverallTimeout { get; set; } = TimeSpan.FromSeconds(300); | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
|
||
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request) | ||
{ | ||
return _client.SendAsync(request); | ||
} | ||
|
||
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken) | ||
{ | ||
return _client.SendAsync(request, completionOption, cancellationToken); | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
46 changes: 46 additions & 0 deletions
46
.../Microsoft.DotNet.MSIdentity/CodeReaderWriter/CodeFiles/Blazor/wasm/UserProfile.razor.txt
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,46 @@ | ||
@page "/profile" | ||
@using Microsoft.AspNetCore.Authorization | ||
@using Microsoft.Graph | ||
@inject Microsoft.Graph.GraphServiceClient GraphServiceClient | ||
@attribute [Authorize] | ||
|
||
<h3>User Profile</h3> | ||
@if (user == null) | ||
{ | ||
<p><em>Loading...</em></p> | ||
} | ||
else | ||
{ | ||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>Property</th> | ||
<th>Value</th> | ||
</tr> | ||
</thead> | ||
<tr> | ||
<td> DisplayName </td> | ||
<td> @user.DisplayName </td> | ||
</tr> | ||
<tr> | ||
<td> UserPrincipalName </td> | ||
<td> @user.UserPrincipalName </td> | ||
</tr> | ||
</table> | ||
} | ||
|
||
@code { | ||
User? user; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
try | ||
{ | ||
user = await GraphServiceClient.Me.Request().GetAsync(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex.Message); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.