Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add web app MVC code modifier (#2314) #2324

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eng/Versions.MSIdentity.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<UsingToolNetFrameworkReferenceAssemblies>true</UsingToolNetFrameworkReferenceAssemblies>
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>2.0.1</VersionPrefix>
<VersionPrefix>2.0.2</VersionPrefix>
<PreReleaseVersionLabel>rtm</PreReleaseVersionLabel>
<IsServicingBuild Condition="'$(PreReleaseVersionLabel)' == 'servicing'">true</IsServicingBuild>
<!--
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@using System.Security.Principal

<ul class="navbar-nav">
@if (User.Identity?.IsAuthenticated == true)
{
<span class="navbar-text text-dark">Hello @User.Identity?.Name!</span>
<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>
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@
}
],
"Methods": {
"OnGet":{
"OnGet":
{
"EditType" : {
"Block": "async Task",
"Options": ["MicrosoftGraph", "DownstreamApi"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,317 @@
{
"Identifier": "dotnet-webapp-mvc",
"Files": [
{
"FileName": "Program.cs",
"Options": [
"MinimalApp"
],
"Methods": {
"Global": {
"CodeChanges": [
{
"Block": "WebApplication.CreateBuilder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)",
"InsertBefore": [
"WebApplication.CreateBuilder.Services.AddControllersWithViews"
],
"LeadingTrivia": {
"Newline": true
}
},
{
"Block": "var initialScopes = WebApplication.CreateBuilder.Configuration[\"DownstreamApi:Scopes\"]?.Split(' ') ?? WebApplication.CreateBuilder.Configuration[\"MicrosoftGraph:Scopes\"]?.Split(' ')",
"Options": [
"DownstreamApi",
"MicrosoftGraph"
],
"InsertBefore": [
"WebApplication.CreateBuilder.Services.AddAuthentication"
],
"LeadingTrivia": {
"Newline": true
}
},
{
"Block": "AddMicrosoftIdentityWebApp(builder.Configuration.GetSection(\"AzureAd\"))",
"Parent": "WebApplication.CreateBuilder.Services.AddAuthentication",
"CodeChangeType": "MemberAccess",
"LeadingTrivia": {
"Newline": true,
"NumberOfSpaces": 4
}
},
{
"MultiLineBlock": [
"var policy = new AuthorizationPolicyBuilder()",
"\t.RequireAuthenticatedUser()",
"\t.Build()"
],
"Parent": "WebApplication.CreateBuilder.Services.AddControllersWithViews",
"CodeChangeType": "Lambda",
"Parameter": "options",
"LeadingTrivia": {
"NumberOfSpaces": 4,
"Newline": true
}
},
{
"Block": "options.Filters.Add(new AuthorizeFilter(policy))",
"Parent": "WebApplication.CreateBuilder.Services.AddControllersWithViews",
"CodeChangeType": "Lambda",
"Parameter": "options",
"LeadingTrivia": {
"NumberOfSpaces": 4
}
},
{
"Block": "EnableTokenAcquisitionToCallDownstreamApi(initialScopes)",
"Parent": "AddMicrosoftIdentityWebApp",
"CodeChangeType": "MemberAccess",
"Options": [
"MicrosoftGraph",
"DownstreamApi"
],
"LeadingTrivia": {
"NumberOfSpaces": 8,
"Newline": true
}
},
{
"MultiLineBlock": [
"AddMicrosoftGraph(builder.Configuration.GetSection(\"MicrosoftGraph\"))",
" .AddInMemoryTokenCaches()"
],
"Parent": "EnableTokenAcquisitionToCallDownstreamApi",
"CodeChangeType": "MemberAccess",
"Options": [
"MicrosoftGraph"
],
"LeadingTrivia": {
"NumberOfSpaces": 12,
"Newline": true
}
},
{
"MultiLineBlock": [
"AddDownstreamWebApi(\"DownstreamApi\",builder.Configuration.GetSection(\"DownstreamApi\"))",
" .AddInMemoryTokenCaches()"
],
"Parent": "EnableTokenAcquisitionToCallDownstreamApi",
"CodeChangeType": "MemberAccess",
"Options": [
"DownstreamApi"
],
"LeadingTrivia": {
"NumberOfSpaces": 12,
"Newline": true
}
},
{
"Block": "WebApplication.CreateBuilder.Services.AddRazorPages()",
"InsertAfter": "WebApplication.CreateBuilder.Services.AddControllersWithViews"
},
{
"Parent": "WebApplication.CreateBuilder.Services.AddRazorPages",
"CodeChangeType": "MemberAccess",
"Block": "AddMicrosoftIdentityUI()",
"LeadingTrivia": {
"Newline": true,
"NumberOfSpaces": 4
}
},
{
"InsertAfter": "app.MapControllerRoute",
"Block": "app.MapRazorPages()"
}
]
}
},
"Usings": [
"Microsoft.AspNetCore.Authentication",
"Microsoft.AspNetCore.Authentication.OpenIdConnect",
"Microsoft.AspNetCore.Authorization",
"Microsoft.AspNetCore.Mvc.Authorization",
"Microsoft.Identity.Web",
"Microsoft.Identity.Web.UI"
]
},
{
"FileName": "_Layout.cshtml",
"Methods": {
"Global": {
"CodeChanges": [
{
"MultiLineBlock": [
"</ul>",
" <partial name=\"_LoginPartial\" />",
" </div>"
],
"ReplaceSnippet": [
"</ul>",
" </div>"
]
}
]
}
}
},
{
"FileName": "LoginPartial.cshtml",
"AddFilePath": "Views/Shared/_LoginPartial.cshtml"
},
{
"FileName": "HomeController.cs",
"ClassAttributes": [
{
"Block": "Authorize"
}
],
"ClassProperties": [
{
"Block": "private readonly GraphServiceClient _graphServiceClient",
"Options": [
"MicrosoftGraph"
]
},
{
"Block": "private readonly IDownstreamWebApi _downstreamWebApi",
"Options": [
"DownstreamApi"
]
}
],
"Methods": {
"HomeController": {
"Parameters": [
"ILogger<HomeController>"
],
"AddParameters": [
{
"Block": "GraphServiceClient graphServiceClient",
"Options": [
"MicrosoftGraph"
]
},
{
"Block": "IDownstreamWebApi downstreamWebApi",
"Options": [
"DownstreamApi"
]
}
],
"CodeChanges": [
{
"Block": "_graphServiceClient = graphServiceClient;",
"Options": [
"MicrosoftGraph"
],
"LeadingTrivia": {
"NumberOfSpaces": 12
}
},
{
"Block": "_downstreamWebApi = downstreamWebApi;",
"Options": [
"DownstreamApi"
],
"LeadingTrivia": {
"NumberOfSpaces": 12
}
}
]
},
"Index": {
"EditType" : {
"Block": "async Task<IActionResult>",
"Options": ["MicrosoftGraph", "DownstreamApi"]
},
"Attributes": [
{
"Block": "AuthorizeForScopes(ScopeKeySection = \"DownstreamApi:Scopes\")",
"Options": [
"DownstreamApi"
]
},
{
"Block": "AuthorizeForScopes(ScopeKeySection = \"MicrosoftGraph:Scopes\")",
"Options": [
"MicrosoftGraph"
]
}
],
"CodeChanges": [
{
"Options": [ "MicrosoftGraph" ],
"Block": " ViewData[\"GraphApiResult\"] = user.DisplayName",
"Prepend": true,
"NumberOfSpaces": 12
},
{
"Options": [ "MicrosoftGraph" ],
"Block": " var user = await _graphServiceClient.Me.Request().GetAsync()",
"Prepend": true,
"NumberOfSpaces": 12
},
{
"Options": [ "DownstreamApi" ],
"Block": " if (response.StatusCode == System.Net.HttpStatusCode.OK)\r\n {\r\n var apiResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false);\r\n ViewData[\"ApiResult\"] = apiResult;\r\n }\r\n else\r\n {\r\n var error = await response.Content.ReadAsStringAsync().ConfigureAwait(false);\r\n throw new HttpRequestException($\"Invalid status code in the HttpResponseMessage: {response.StatusCode}: {error}\");\r\n }\r\n",
"Prepend": true,
"NumberOfSpaces": 12
},
{
"Options": [ "DownstreamApi" ],
"Block": " using var response = await _downstreamWebApi.CallWebApiForUserAsync(\"DownstreamApi\").ConfigureAwait(false)",
"Prepend": true,
"NumberOfSpaces": 12
}
]
},
"Error": {
"Attributes": [
{
"Block": "AllowAnonymous"
}
]
}
},
"Usings": [
"Microsoft.AspNetCore.Authorization"
],
"UsingsWithOptions": [
{
"Block": "Microsoft.Graph",
"Options": [ "MicrosoftGraph" ]
},
{
"Block": "Microsoft.Identity.Web",
"Options": ["DownstreamApi", "MicrosoftGraph"]
}
]
},
{
"FileName": "Index.cshtml",
"Options": [
"MicrosoftGraph",
"DownstreamApi"
],
"Methods": {
"Global": {
"CodeChanges": [
{
"Block": "\n<div>API result</div>\n<div>@ViewData[\"ApiResult\"]</div>\n",
"Options": [
"DownstreamApi"
]
},
{
"Block": "\n<div>Graph API result</div>\n<div>@ViewData[\"GraphApiResult\"]</div>\n",
"Options": [
"MicrosoftGraph"
]
}
]
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public static class ProjectTypes
{
public const string BlazorServer = "blazorserver";
public const string WebApp = "webapp";
public const string WebAppMvc = "webapp-mvc";
public const string WebApi = "webapi";
public const string BlazorWasmClient = "blazorwasm-client";
public const string BlazorWasm = "blazorwasm";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@
"ProjectRelativeFolder": "",
"BasedOnProjectDescription": "dotnet-web",
"MatchesForProjectType": [
{
"FileRelativePath": "Startup.cs",
"MatchAny": [ ".AddAzureAD", ".AddMicrosoftIdentityWebApp", ".AddMicrosoftIdentityWebAppAuthentication", "Microsoft.Owin" ]
},
{
"FolderRelativePath": "Views",
"Sets": "IsWebApp"
},
{
"FolderRelativePath": "Pages",
"FileExtension": ".cshtml",
"FileRelativePath": "Index.cshtml",
"Sets": "IsWebApp"
}
],
Expand Down
Loading