diff --git a/TwilioSMSReceiver.Web/Controllers/HomeController.cs b/TwilioSMSReceiver.Web/Controllers/HomeController.cs new file mode 100644 index 0000000..2aad992 --- /dev/null +++ b/TwilioSMSReceiver.Web/Controllers/HomeController.cs @@ -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 _logger; + + public HomeController(ILogger 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 }); + } + } +} \ No newline at end of file diff --git a/TwilioSMSReceiver.Web/Models/ErrorViewModel.cs b/TwilioSMSReceiver.Web/Models/ErrorViewModel.cs new file mode 100644 index 0000000..3b0afbd --- /dev/null +++ b/TwilioSMSReceiver.Web/Models/ErrorViewModel.cs @@ -0,0 +1,9 @@ +namespace TwilioSMSReceiver.Web.Models +{ + public class ErrorViewModel + { + public string? RequestId { get; set; } + + public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); + } +} \ No newline at end of file diff --git a/TwilioSMSReceiver.Web/Pages/Error.cshtml.cs b/TwilioSMSReceiver.Web/Pages/Error.cshtml.cs deleted file mode 100644 index 3e06ad7..0000000 --- a/TwilioSMSReceiver.Web/Pages/Error.cshtml.cs +++ /dev/null @@ -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 _logger; - - public ErrorModel(ILogger logger) - { - _logger = logger; - } - - public void OnGet() - { - RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; - } - } -} \ No newline at end of file diff --git a/TwilioSMSReceiver.Web/Pages/Index.cshtml.cs b/TwilioSMSReceiver.Web/Pages/Index.cshtml.cs deleted file mode 100644 index 403577a..0000000 --- a/TwilioSMSReceiver.Web/Pages/Index.cshtml.cs +++ /dev/null @@ -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 _logger; - - public IndexModel(ILogger logger, GraphServiceClient graphServiceClient) - { - _logger = logger; - _graphServiceClient = graphServiceClient; - } - - public async Task OnGet() - { - var user = await _graphServiceClient.Me.Request().GetAsync(); - ViewData["GraphApiResult"] = user.DisplayName; - - } - } -} \ No newline at end of file diff --git a/TwilioSMSReceiver.Web/Pages/Privacy.cshtml.cs b/TwilioSMSReceiver.Web/Pages/Privacy.cshtml.cs deleted file mode 100644 index 1beba5a..0000000 --- a/TwilioSMSReceiver.Web/Pages/Privacy.cshtml.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; - -namespace TwilioSMSReceiver.Web.Pages -{ - public class PrivacyModel : PageModel - { - private readonly ILogger _logger; - - public PrivacyModel(ILogger logger) - { - _logger = logger; - } - - public void OnGet() - { - } - } -} \ No newline at end of file diff --git a/TwilioSMSReceiver.Web/Program.cs b/TwilioSMSReceiver.Web/Program.cs index 367f849..4d6459d 100644 --- a/TwilioSMSReceiver.Web/Program.cs +++ b/TwilioSMSReceiver.Web/Program.cs @@ -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(); diff --git a/TwilioSMSReceiver.Web/Properties/launchSettings.json b/TwilioSMSReceiver.Web/Properties/launchSettings.json index 3a63146..e7c1d31 100644 --- a/TwilioSMSReceiver.Web/Properties/launchSettings.json +++ b/TwilioSMSReceiver.Web/Properties/launchSettings.json @@ -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" } diff --git a/TwilioSMSReceiver.Web/Properties/serviceDependencies.json b/TwilioSMSReceiver.Web/Properties/serviceDependencies.json index b4889d2..e75a2d1 100644 --- a/TwilioSMSReceiver.Web/Properties/serviceDependencies.json +++ b/TwilioSMSReceiver.Web/Properties/serviceDependencies.json @@ -1,11 +1,11 @@ { "dependencies": { + "secrets1": { + "type": "secrets" + }, "identityapp.aad1": { "type": "identityapp.aad", "connectionId": "AzureAD:ClientSecret" - }, - "secrets1": { - "type": "secrets" } } } \ No newline at end of file diff --git a/TwilioSMSReceiver.Web/Properties/serviceDependencies.local.json b/TwilioSMSReceiver.Web/Properties/serviceDependencies.local.json index a351052..02ce0c0 100644 --- a/TwilioSMSReceiver.Web/Properties/serviceDependencies.local.json +++ b/TwilioSMSReceiver.Web/Properties/serviceDependencies.local.json @@ -1,12 +1,12 @@ { "dependencies": { + "secrets1": { + "type": "secrets.user" + }, "identityapp.aad1": { "secretStore": "LocalSecretsFile", "type": "identityapp.aad.callsgraph", "connectionId": "AzureAD:ClientSecret" - }, - "secrets1": { - "type": "secrets.user" } } } \ No newline at end of file diff --git a/TwilioSMSReceiver.Web/TwilioSMSReceiver.Web.csproj b/TwilioSMSReceiver.Web/TwilioSMSReceiver.Web.csproj index 2b1ce4a..9669239 100644 --- a/TwilioSMSReceiver.Web/TwilioSMSReceiver.Web.csproj +++ b/TwilioSMSReceiver.Web/TwilioSMSReceiver.Web.csproj @@ -4,7 +4,7 @@ net6.0 enable enable - aspnet-TwilioSMSReceiver.Web-36C83D20-F452-4BB4-99DE-C484F46D03ED + aspnet-TwilioSMSReceiver.Web-EAF5A268-6EE9-435C-814A-0FE45182EC8D diff --git a/TwilioSMSReceiver.Web/Pages/Index.cshtml b/TwilioSMSReceiver.Web/Views/Home/Index.cshtml similarity index 80% rename from TwilioSMSReceiver.Web/Pages/Index.cshtml rename to TwilioSMSReceiver.Web/Views/Home/Index.cshtml index 31c7bf4..e00919e 100644 --- a/TwilioSMSReceiver.Web/Pages/Index.cshtml +++ b/TwilioSMSReceiver.Web/Views/Home/Index.cshtml @@ -1,7 +1,5 @@ -@page -@model IndexModel @{ - ViewData["Title"] = "Home page"; + ViewData["Title"] = "Home Page"; }
diff --git a/TwilioSMSReceiver.Web/Pages/Privacy.cshtml b/TwilioSMSReceiver.Web/Views/Home/Privacy.cshtml similarity index 80% rename from TwilioSMSReceiver.Web/Pages/Privacy.cshtml rename to TwilioSMSReceiver.Web/Views/Home/Privacy.cshtml index 46ba966..af4fb19 100644 --- a/TwilioSMSReceiver.Web/Pages/Privacy.cshtml +++ b/TwilioSMSReceiver.Web/Views/Home/Privacy.cshtml @@ -1,6 +1,4 @@ -@page -@model PrivacyModel -@{ +@{ ViewData["Title"] = "Privacy Policy"; }

@ViewData["Title"]

diff --git a/TwilioSMSReceiver.Web/Pages/Error.cshtml b/TwilioSMSReceiver.Web/Views/Shared/Error.cshtml similarity index 70% rename from TwilioSMSReceiver.Web/Pages/Error.cshtml rename to TwilioSMSReceiver.Web/Views/Shared/Error.cshtml index 6f92b95..10cf32b 100644 --- a/TwilioSMSReceiver.Web/Pages/Error.cshtml +++ b/TwilioSMSReceiver.Web/Views/Shared/Error.cshtml @@ -1,5 +1,4 @@ -@page -@model ErrorModel +@model ErrorViewModel @{ ViewData["Title"] = "Error"; } @@ -7,16 +6,16 @@

Error.

An error occurred while processing your request.

-@if (Model.ShowRequestId) +@if (Model?.ShowRequestId ?? false) {

- Request ID: @Model.RequestId + Request ID: @Model?.RequestId

}

Development Mode

- Swapping to the Development environment displays detailed information about the error that occurred. + Swapping to Development environment will display more detailed information about the error that occurred.

The Development environment shouldn't be enabled for deployed applications. diff --git a/TwilioSMSReceiver.Web/Pages/Shared/_Layout.cshtml b/TwilioSMSReceiver.Web/Views/Shared/_Layout.cshtml similarity index 85% rename from TwilioSMSReceiver.Web/Pages/Shared/_Layout.cshtml rename to TwilioSMSReceiver.Web/Views/Shared/_Layout.cshtml index e7d9f7d..ce14bc3 100644 --- a/TwilioSMSReceiver.Web/Pages/Shared/_Layout.cshtml +++ b/TwilioSMSReceiver.Web/Views/Shared/_Layout.cshtml @@ -11,8 +11,8 @@