Switch to MVC

This commit is contained in:
Jeff Leung 2021-12-30 22:23:05 -08:00
parent 74001ae291
commit 6a9623d336
21 changed files with 83 additions and 113 deletions

View File

@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using TwilioSMSReceiver.Web.Models;
namespace TwilioSMSReceiver.Web.Controllers
{
[Authorize]
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
public IActionResult Privacy()
{
return View();
}
[AllowAnonymous]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}

View File

@ -0,0 +1,9 @@
namespace TwilioSMSReceiver.Web.Models
{
public class ErrorViewModel
{
public string? RequestId { get; set; }
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}

View File

@ -1,27 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Diagnostics;
namespace TwilioSMSReceiver.Web.Pages
{
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
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;
}
}
}

View File

@ -1,28 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Identity.Web;
using System.Net;
using Microsoft.Graph;
namespace TwilioSMSReceiver.Web.Pages
{
[AuthorizeForScopes(ScopeKeySection = "MicrosoftGraph:Scopes")]
public class IndexModel : PageModel
{
private readonly GraphServiceClient _graphServiceClient;
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger, GraphServiceClient graphServiceClient)
{
_logger = logger;
_graphServiceClient = graphServiceClient;
}
public async Task OnGet()
{
var user = await _graphServiceClient.Me.Request().GetAsync();
ViewData["GraphApiResult"] = user.DisplayName;
}
}
}

View File

@ -1,19 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace TwilioSMSReceiver.Web.Pages
{
public class PrivacyModel : PageModel
{
private readonly ILogger<PrivacyModel> _logger;
public PrivacyModel(ILogger<PrivacyModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
}
}

View File

@ -17,9 +17,15 @@ builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
builder.Services.AddAuthorization(options => builder.Services.AddAuthorization(options =>
{ {
// By default, all incoming requests will be authorized according to the default policy.
options.FallbackPolicy = options.DefaultPolicy; options.FallbackPolicy = options.DefaultPolicy;
}); });
builder.Services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
builder.Services.AddRazorPages() builder.Services.AddRazorPages()
.AddMicrosoftIdentityUI(); .AddMicrosoftIdentityUI();
@ -28,7 +34,7 @@ var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {
app.UseExceptionHandler("/Error"); app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts(); app.UseHsts();
} }
@ -41,7 +47,9 @@ app.UseRouting();
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages(); app.MapRazorPages();
app.MapControllers(); app.MapControllers();
app.Run(); app.Run();

View File

@ -3,8 +3,8 @@
"windowsAuthentication": false, "windowsAuthentication": false,
"anonymousAuthentication": true, "anonymousAuthentication": true,
"iisExpress": { "iisExpress": {
"applicationUrl": "http://localhost:3716", "applicationUrl": "http://localhost:46828",
"sslPort": 44304 "sslPort": 44370
} }
}, },
"profiles": { "profiles": {
@ -12,7 +12,7 @@
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": true,
"applicationUrl": "https://localhost:7012;http://localhost:5062", "applicationUrl": "https://localhost:7213;http://localhost:5213",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }

View File

@ -1,11 +1,11 @@
{ {
"dependencies": { "dependencies": {
"secrets1": {
"type": "secrets"
},
"identityapp.aad1": { "identityapp.aad1": {
"type": "identityapp.aad", "type": "identityapp.aad",
"connectionId": "AzureAD:ClientSecret" "connectionId": "AzureAD:ClientSecret"
},
"secrets1": {
"type": "secrets"
} }
} }
} }

View File

@ -1,12 +1,12 @@
{ {
"dependencies": { "dependencies": {
"secrets1": {
"type": "secrets.user"
},
"identityapp.aad1": { "identityapp.aad1": {
"secretStore": "LocalSecretsFile", "secretStore": "LocalSecretsFile",
"type": "identityapp.aad.callsgraph", "type": "identityapp.aad.callsgraph",
"connectionId": "AzureAD:ClientSecret" "connectionId": "AzureAD:ClientSecret"
},
"secrets1": {
"type": "secrets.user"
} }
} }
} }

View File

@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>aspnet-TwilioSMSReceiver.Web-36C83D20-F452-4BB4-99DE-C484F46D03ED</UserSecretsId> <UserSecretsId>aspnet-TwilioSMSReceiver.Web-EAF5A268-6EE9-435C-814A-0FE45182EC8D</UserSecretsId>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,7 +1,5 @@
@page
@model IndexModel
@{ @{
ViewData["Title"] = "Home page"; ViewData["Title"] = "Home Page";
} }
<div class="text-center"> <div class="text-center">

View File

@ -1,6 +1,4 @@
@page @{
@model PrivacyModel
@{
ViewData["Title"] = "Privacy Policy"; ViewData["Title"] = "Privacy Policy";
} }
<h1>@ViewData["Title"]</h1> <h1>@ViewData["Title"]</h1>

View File

@ -1,5 +1,4 @@
@page @model ErrorViewModel
@model ErrorModel
@{ @{
ViewData["Title"] = "Error"; ViewData["Title"] = "Error";
} }
@ -7,16 +6,16 @@
<h1 class="text-danger">Error.</h1> <h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2> <h2 class="text-danger">An error occurred while processing your request.</h2>
@if (Model.ShowRequestId) @if (Model?.ShowRequestId ?? false)
{ {
<p> <p>
<strong>Request ID:</strong> <code>@Model.RequestId</code> <strong>Request ID:</strong> <code>@Model?.RequestId</code>
</p> </p>
} }
<h3>Development Mode</h3> <h3>Development Mode</h3>
<p> <p>
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred. Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
</p> </p>
<p> <p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong> <strong>The Development environment shouldn't be enabled for deployed applications.</strong>

View File

@ -11,8 +11,8 @@
<body> <body>
<header> <header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"> <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container"> <div class="container-fluid">
<a class="navbar-brand" asp-area="" asp-page="/Index">TwilioSMSReceiver.Web</a> <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">TwilioSMSReceiver.Web</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent" <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation"> aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span> <span class="navbar-toggler-icon"></span>
@ -20,10 +20,10 @@
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between"> <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1"> <ul class="navbar-nav flex-grow-1">
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a> <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a> <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li> </li>
</ul> </ul>
<partial name="_LoginPartial" /> <partial name="_LoginPartial" />
@ -39,14 +39,12 @@
<footer class="border-top footer text-muted"> <footer class="border-top footer text-muted">
<div class="container"> <div class="container">
&copy; 2021 - TwilioSMSReceiver.Web - <a asp-area="" asp-page="/Privacy">Privacy</a> &copy; 2021 - TwilioSMSReceiver.Web - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div> </div>
</footer> </footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script> <script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script> <script src="~/js/site.js" asp-append-version="true"></script>
@await RenderSectionAsync("Scripts", required: false) @await RenderSectionAsync("Scripts", required: false)
</body> </body>
</html> </html>

View File

@ -1,3 +1,3 @@
@using TwilioSMSReceiver.Web @using TwilioSMSReceiver.Web
@namespace TwilioSMSReceiver.Web.Pages @using TwilioSMSReceiver.Web.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

View File

@ -1,5 +1,4 @@
{ {
"DetailedErrors": true,
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",

View File

@ -12,7 +12,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TwilioSMSReceiver.Data", "T
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TwilioSMSReceiver.Common", "TwilioSMSReceiver.Common\TwilioSMSReceiver.Common.csproj", "{09A6960F-AA69-4EF9-94DC-9BA5FDBB4E4A}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TwilioSMSReceiver.Common", "TwilioSMSReceiver.Common\TwilioSMSReceiver.Common.csproj", "{09A6960F-AA69-4EF9-94DC-9BA5FDBB4E4A}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TwilioSMSReceiver.Web", "TwilioSMSReceiver.Web\TwilioSMSReceiver.Web.csproj", "{1AE878FA-D91B-4AA0-B782-66DDA2EC3BB2}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TwilioSMSReceiver.Web", "TwilioSMSReceiver.Web\TwilioSMSReceiver.Web.csproj", "{25E485DD-0D95-48C0-BC90-B24FB1415F30}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -32,10 +32,10 @@ Global
{09A6960F-AA69-4EF9-94DC-9BA5FDBB4E4A}.Debug|Any CPU.Build.0 = Debug|Any CPU {09A6960F-AA69-4EF9-94DC-9BA5FDBB4E4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{09A6960F-AA69-4EF9-94DC-9BA5FDBB4E4A}.Release|Any CPU.ActiveCfg = Release|Any CPU {09A6960F-AA69-4EF9-94DC-9BA5FDBB4E4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{09A6960F-AA69-4EF9-94DC-9BA5FDBB4E4A}.Release|Any CPU.Build.0 = Release|Any CPU {09A6960F-AA69-4EF9-94DC-9BA5FDBB4E4A}.Release|Any CPU.Build.0 = Release|Any CPU
{1AE878FA-D91B-4AA0-B782-66DDA2EC3BB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {25E485DD-0D95-48C0-BC90-B24FB1415F30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1AE878FA-D91B-4AA0-B782-66DDA2EC3BB2}.Debug|Any CPU.Build.0 = Debug|Any CPU {25E485DD-0D95-48C0-BC90-B24FB1415F30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AE878FA-D91B-4AA0-B782-66DDA2EC3BB2}.Release|Any CPU.ActiveCfg = Release|Any CPU {25E485DD-0D95-48C0-BC90-B24FB1415F30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1AE878FA-D91B-4AA0-B782-66DDA2EC3BB2}.Release|Any CPU.Build.0 = Release|Any CPU {25E485DD-0D95-48C0-BC90-B24FB1415F30}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE