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 =>
{
// By default, all incoming requests will be authorized according to the default policy.
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
builder.Services.AddRazorPages()
.AddMicrosoftIdentityUI();
@ -28,7 +34,7 @@ var app = builder.Build();
// Configure the HTTP request pipeline.
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.
app.UseHsts();
}
@ -41,7 +47,9 @@ app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.MapControllers();
app.Run();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -11,8 +11,8 @@
<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">TwilioSMSReceiver.Web</a>
<div class="container-fluid">
<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"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
@ -20,10 +20,10 @@
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<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 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>
</ul>
<partial name="_LoginPartial" />
@ -39,14 +39,12 @@
<footer class="border-top footer text-muted">
<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>
</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>

View File

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

View File

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

View File

@ -12,7 +12,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TwilioSMSReceiver.Data", "T
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TwilioSMSReceiver.Common", "TwilioSMSReceiver.Common\TwilioSMSReceiver.Common.csproj", "{09A6960F-AA69-4EF9-94DC-9BA5FDBB4E4A}"
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
Global
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}.Release|Any CPU.ActiveCfg = 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
{1AE878FA-D91B-4AA0-B782-66DDA2EC3BB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AE878FA-D91B-4AA0-B782-66DDA2EC3BB2}.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}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{25E485DD-0D95-48C0-BC90-B24FB1415F30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{25E485DD-0D95-48C0-BC90-B24FB1415F30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{25E485DD-0D95-48C0-BC90-B24FB1415F30}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE