Skip to content

Commit

Permalink
Enable Microsoft Graph option for Blazor Server (#1719)
Browse files Browse the repository at this point in the history
Enable Microsoft Graph option for Blazor Server, add .razor modification functionality
  • Loading branch information
zahalzel authored Dec 13, 2021
1 parent 402c358 commit 7904105
Show file tree
Hide file tree
Showing 15 changed files with 619 additions and 285 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
@page "/showprofile"

@using Microsoft.Identity.Web
@using Microsoft.Graph
@inject Microsoft.Graph.GraphServiceClient GraphServiceClient
@inject MicrosoftIdentityConsentAndConditionalAccessHandler ConsentHandler

<h1>Me</h1>

<p>This component demonstrates fetching data from a service.</p>

@if (user == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table table-striped table-condensed" style="font-family: monospace">
<tr>
<th>Property</th>
<th>Value</th>
</tr>
<tr>
<td>Name</td>
<td>@user.DisplayName</td>
</tr>
<tr>
<td>Photo</td>
<td>
@{
if (photo != null)
{
<img style="margin: 5px 0; width: 150px" src="data:image/jpeg;base64, @photo" />
}
else
{
<h3>NO PHOTO</h3>
<p>Check user profile in Azure Active Directory to add a photo.</p>
}
}
</td>
</tr>
</table>
}

@code {
User user;
string photo;

protected override async Task OnInitializedAsync()
{
try
{
user = await GraphServiceClient.Me.Request().GetAsync();
photo = await GetPhoto();
}
catch (Exception ex)
{
ConsentHandler.HandleException(ex);
}
}

protected async Task<string> GetPhoto()
{
string photo;

try
{
using (var photoStream = await GraphServiceClient.Me.Photo.Content.Request().GetAsync())
{
byte[] photoByte = ((System.IO.MemoryStream)photoStream).ToArray();
photo = Convert.ToBase64String(photoByte);
this.StateHasChanged();
}

}
catch (Exception)
{
photo = null;
}
return photo;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ach project type/scenario should have a json config file for itself.
Each project type/scenario should have a json config file for itself.

class CodeModifierConfig
{
Expand Down Expand Up @@ -27,6 +27,7 @@ class CodeChange
public string? Parent { get; set; } --> Add C# statement syntax node upon this parent statement syntax node based on Type
public string? Type { get; set; } --> CodeChangeType (below) string.
public bool? Append { get; set; } = false; --> Insert Block at the top of the method.
public string ReplaceSnippet { get; set; } --> Replace snippet with Block
}

class CodeChangeType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,131 @@
"Identifier": "dotnet-blazorserver",
"Files": [
{
"FileName": "Startup.cs",
"FileName": "Program.cs",
"Methods": {
"Configure": {
"Parameters": [ "IApplicationBuilder", "IWebHostEnvironment" ],
"Global": {
"CodeChanges": [
{
"InsertAfter": "IApplicationBuilder.UseRouting()",
"Block": "IApplicationBuilder.UseAuthentication()"
"Block": "var initialScopes = WebApplication.CreateBuilder.Configuration[\"DownstreamApi:Scopes\"]?.Split(' ') ?? WebApplication.CreateBuilder.Configuration[\"MicrosoftGraph:Scopes\"]?.Split(' ');\n",
"Options": [
"DownstreamApi",
"MicrosoftGraph"
],
"InsertBefore": [
"WebApplication.CreateBuilder.Services.AddAuthentication",
"WebApplication.CreateBuilder.Services.AddAuthorization",
"WebApplication.CreateBuilder.Services.AddRazorPages()",
"WebApplication.CreateBuilder.Build()"
],
"CodeFormatting": {
"Newline": true
}
},
{
"InsertAfter": "IApplicationBuilder.UseAuthentication()",
"Block": "IApplicationBuilder.UseAuthorization()"
"Block": "WebApplication.CreateBuilder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)\n .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection(\"AzureAd\"));",
"InsertBefore": [
"WebApplication.CreateBuilder.Services.AddAuthorization",
"WebApplication.CreateBuilder.Services.AddRazorPages()",
"WebApplication.CreateBuilder.Build()"
],
"CodeFormatting": {
"Newline": true
}
},
{
"Parent": "IApplicationBuilder.UseEndpoints",
"Type": "InLambdaBlock",
"Block": "endpoints.MapControllers()"
}
]
},
"ConfigureServices": {
"Parameters": [ "IServiceCollection" ],
"CodeChanges": [
"Parent": "AddMicrosoftIdentityWebApp",
"Options": [
"MemberAccess",
"MicrosoftGraph",
"DownstreamApi"
],
"Block": "EnableTokenAcquisitionToCallDownstreamApi(initialScopes)",
"CodeFormatting": {
"NumberOfSpaces": 8,
"Newline": true
}
},
{
"Parent": "EnableTokenAcquisitionToCallDownstreamApi(initialScopes)",
"Options": [
"MemberAccess",
"MicrosoftGraph"
],
"Block": "AddMicrosoftGraph(builder.Configuration.GetSection(\"MicrosoftGraph\"))",
"CodeFormatting": {
"NumberOfSpaces": 12,
"Newline": true
}
},
{
"Parent": "EnableTokenAcquisitionToCallDownstreamApi(initialScopes)",
"Options": [
"MemberAccess",
"DownstreamApi"
],
"Block": "AddDownstreamWebApi(\"DownstreamApi\",builder.Configuration.GetSection(\"DownstreamApi\"))",
"CodeFormatting": {
"NumberOfSpaces": 12,
"Newline": true
}
},
{
"Block": "IServiceCollection.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)"
"Parent": "AddDownstreamWebApi",
"Options": [
"MemberAccess",
"DownstreamApi"
],
"Block": "AddInMemoryTokenCaches()",
"CodeFormatting": {
"NumberOfSpaces": 12,
"Newline": true
}
},
{
"Block": "IServiceCollection.AddControllersWithViews().AddMicrosoftIdentityUI()",
"InsertAfter":"IApplicationBuilder.AddAuthentication()"
"Parent": "AddMicrosoftGraph",
"Options": [
"MemberAccess",
"MicrosoftGraph"
],
"Block": "AddInMemoryTokenCaches()",
"CodeFormatting": {
"NumberOfSpaces": 12,
"Newline": true
}
},
{
"Parent": "IServiceCollection.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)",
"Type": "MemberAccess",
"Block": "AddMicrosoftIdentityWebApp(Configuration.GetSection(\"AzureAd\"))"
"CheckBlock": "WebApplication.CreateBuilder.Services.AddAuthorization",
"Block": "WebApplication.CreateBuilder.Services.AddAuthorization(options =>\n{\n options.FallbackPolicy = options.DefaultPolicy;\n});\n\r",
"InsertAfter": "WebApplication.CreateBuilder.Services.AddAuthentication",
"InsertBefore": [
"WebApplication.CreateBuilder.Services.AddRazorPages()",
"WebApplication.CreateBuilder.Build()"
],
"CodeFormatting": {
"Newline": true
}
},
{
"Block": "IServiceCollection.AddAuthorization(options => { options.FallbackPolicy = options.DefaultPolicy; })"
"Parent": "WebApplication.CreateBuilder.Services.AddRazorPages()",
"Options": [
"MemberAccess"
],
"Block": "AddMicrosoftIdentityUI()"
},
{
"Block": "IServiceCollection.AddServerSideBlazor()",
"InsertAfter":"IServiceCollection.AddRazorPages()"
"InsertAfter": "app.UseRouting();",
"Block": "app.UseAuthentication();",
"InsertBefore": [
"app.UseAuthorization();",
"app.Run();"
]
},
{
"Parent": "IServiceCollection.AddServerSideBlazor()",
"Type": "MemberAccess",
"Block": "AddMicrosoftIdentityConsentHandler()"
"InsertAfter": "app.MapRazorPages();",
"Block": "app.MapControllers();",
"InsertBefore": [
"app.Run();"
]
}
]
}
Expand All @@ -60,6 +139,29 @@
"Microsoft.AspNetCore.Authorization",
"Microsoft.AspNetCore.Mvc.Authorization"
]
},
{
"FileName": "NavMenu.razor",
"Options": [
"MicrosoftGraph",
"DownstreamApi"
],
"RazorChanges": [
{
"Block": "</NavLink>\r\n </div>\r\n <div class=\"nav-item px-3\">\r\n <NavLink class=\"nav-link\" href=\"showprofile\">\r\n <span class=\"oi oi-list-rich\" aria-hidden=\"true\"></span> Show profile\r\n </NavLink>\r\n </div>\r\n </nav>\r\n</div>",
"ReplaceSnippet": "</NavLink>\r\n </div>\r\n </nav>\r\n</div>",
"Options": [
"MicrosoftGraph"
]
}
]
},
{
"FileName": "ShowProfile.razor",
"AddFilePath": "Pages/ShowProfile.razor",
"Options": [
"MicrosoftGraph"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.DotNet.MSIdentity.Project;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand All @@ -10,6 +9,7 @@
using System.Reflection;
using System.Text.Json;
using System.Xml;
using Microsoft.DotNet.MSIdentity.Project;
using ConfigurationProperties = Microsoft.DotNet.MSIdentity.Project.ConfigurationProperties;

namespace Microsoft.DotNet.MSIdentity.CodeReaderWriter
Expand Down
Loading

0 comments on commit 7904105

Please sign in to comment.