Skip to content

Commit 5f47672

Browse files
committed
Add project files.
1 parent dec515a commit 5f47672

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+40232
-0
lines changed

asp-net-core-filters.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30803.129
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "asp-net-core-filters", "asp-net-core-filters\asp-net-core-filters.csproj", "{0EAB1F5C-6249-4AE4-A206-4D40C32E96BD}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{0EAB1F5C-6249-4AE4-A206-4D40C32E96BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{0EAB1F5C-6249-4AE4-A206-4D40C32E96BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{0EAB1F5C-6249-4AE4-A206-4D40C32E96BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{0EAB1F5C-6249-4AE4-A206-4D40C32E96BD}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {16F2D505-87F4-4BDF-95B8-5900134B1712}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using asp_net_core_filters.Filters;
2+
using asp_net_core_filters.Models;
3+
using Microsoft.AspNetCore.Authorization;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Diagnostics;
10+
using System.Linq;
11+
using System.Security.Claims;
12+
using System.Threading.Tasks;
13+
14+
namespace asp_net_core_filters.Controllers
15+
{
16+
public class HomeController : Controller
17+
{
18+
private readonly ILogger<HomeController> _logger;
19+
20+
public HomeController(ILogger<HomeController> logger)
21+
{
22+
_logger = logger;
23+
}
24+
25+
[ServiceFilter(typeof(AuthorizeIPAddress))]
26+
public IActionResult Index()
27+
{
28+
return View();
29+
}
30+
31+
public IActionResult Privacy()
32+
{
33+
return View();
34+
}
35+
36+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
37+
public IActionResult Error()
38+
{
39+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
40+
}
41+
}
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.Filters;
4+
using System.Net;
5+
6+
namespace asp_net_core_filters.Filters
7+
{
8+
public class AuthorizeIPAddress : IAuthorizationFilter
9+
{
10+
private readonly string _allowedIPAddress;
11+
public AuthorizeIPAddress(string allowedIPAddress)
12+
{
13+
this._allowedIPAddress = allowedIPAddress;
14+
}
15+
16+
public void OnAuthorization(AuthorizationFilterContext context)
17+
{
18+
var requestIp = context.HttpContext.Connection.RemoteIpAddress;
19+
20+
var ipAddresses = this._allowedIPAddress.Split(';');
21+
var unauthorizedIp = true;
22+
23+
if (requestIp.IsIPv4MappedToIPv6)
24+
{
25+
requestIp = requestIp.MapToIPv4();
26+
}
27+
28+
foreach (var address in ipAddresses)
29+
{
30+
var testIp = IPAddress.Parse(address);
31+
32+
if (testIp.Equals(requestIp))
33+
{
34+
unauthorizedIp = false;
35+
break;
36+
}
37+
}
38+
39+
if (unauthorizedIp)
40+
{
41+
context.Result = new StatusCodeResult(StatusCodes.Status403Forbidden);
42+
return;
43+
}
44+
}
45+
}
46+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Mvc.Filters;
3+
using System.Collections.Generic;
4+
5+
namespace asp_net_core_filters.Filters
6+
{
7+
public class CacheResourceFilter : IResourceFilter
8+
{
9+
private static readonly Dictionary<string, object> _cache
10+
= new Dictionary<string, object>();
11+
private string _cacheKey;
12+
public void OnResourceExecuting(ResourceExecutingContext context)
13+
{
14+
_cacheKey = context.HttpContext.Request.Path.ToString();
15+
if (_cache.ContainsKey(_cacheKey))
16+
{
17+
var cachedValue = _cache[_cacheKey] as string;
18+
if (cachedValue != null)
19+
{
20+
context.Result = new ContentResult()
21+
{ Content = cachedValue };
22+
}
23+
}
24+
}
25+
public void OnResourceExecuted(ResourceExecutedContext context)
26+
{
27+
if (!string.IsNullOrEmpty(_cacheKey) && !_cache.ContainsKey(_cacheKey))
28+
{
29+
var result = context.Result as ContentResult;
30+
if (result != null)
31+
{
32+
_cache.Add(_cacheKey, result.Content);
33+
}
34+
}
35+
}
36+
}
37+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace asp_net_core_filters.Models
4+
{
5+
public class ErrorViewModel
6+
{
7+
public string RequestId { get; set; }
8+
9+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10+
}
11+
}

asp-net-core-filters/Program.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Logging;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
namespace asp_net_core_filters
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:54001",
7+
"sslPort": 44370
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"asp_net_core_filters": {
19+
"commandName": "Project",
20+
"dotnetRunMessages": "true",
21+
"launchBrowser": true,
22+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
}
26+
}
27+
}
28+
}

asp-net-core-filters/Startup.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using asp_net_core_filters.Filters;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.HttpsPolicy;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Hosting;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
using System.Threading.Tasks;
13+
14+
namespace asp_net_core_filters
15+
{
16+
public class Startup
17+
{
18+
public Startup(IConfiguration configuration)
19+
{
20+
Configuration = configuration;
21+
}
22+
23+
public IConfiguration Configuration { get; }
24+
25+
// This method gets called by the runtime. Use this method to add services to the container.
26+
public void ConfigureServices(IServiceCollection services)
27+
{
28+
services.AddControllersWithViews();
29+
services.AddScoped<AuthorizeIPAddress>(container =>
30+
{
31+
//test for valid authorization
32+
//return new AuthorizeIPAddress("127.0.0.1;192.168.1.5;::1");
33+
34+
//test for invalid authorization
35+
return new AuthorizeIPAddress("000.0.0.0");
36+
});
37+
}
38+
39+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
40+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
41+
{
42+
if (env.IsDevelopment())
43+
{
44+
app.UseDeveloperExceptionPage();
45+
}
46+
else
47+
{
48+
app.UseExceptionHandler("/Home/Error");
49+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
50+
app.UseHsts();
51+
}
52+
app.UseHttpsRedirection();
53+
app.UseStaticFiles();
54+
55+
app.UseRouting();
56+
57+
app.UseAuthorization();
58+
59+
app.UseEndpoints(endpoints =>
60+
{
61+
endpoints.MapControllerRoute(
62+
name: "default",
63+
pattern: "{controller=Home}/{action=Index}/{id?}");
64+
});
65+
}
66+
}
67+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div class="text-center">
6+
<h1 class="display-4">Welcome</h1>
7+
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
8+
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@model ErrorViewModel
2+
@{
3+
ViewData["Title"] = "Error";
4+
}
5+
6+
<h1 class="text-danger">Error.</h1>
7+
<h2 class="text-danger">An error occurred while processing your request.</h2>
8+
9+
@if (Model.ShowRequestId)
10+
{
11+
<p>
12+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
13+
</p>
14+
}
15+
16+
<h3>Development Mode</h3>
17+
<p>
18+
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
19+
</p>
20+
<p>
21+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
22+
It can result in displaying sensitive information from exceptions to end users.
23+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
24+
and restarting the app.
25+
</p>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>@ViewData["Title"] - asp_net_core_filters</title>
7+
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
8+
<link rel="stylesheet" href="~/css/site.css" />
9+
</head>
10+
<body>
11+
<header>
12+
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
13+
<div class="container">
14+
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">asp_net_core_filters</a>
15+
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
16+
aria-expanded="false" aria-label="Toggle navigation">
17+
<span class="navbar-toggler-icon"></span>
18+
</button>
19+
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
20+
<ul class="navbar-nav flex-grow-1">
21+
<li class="nav-item">
22+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
23+
</li>
24+
<li class="nav-item">
25+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
26+
</li>
27+
</ul>
28+
</div>
29+
</div>
30+
</nav>
31+
</header>
32+
<div class="container">
33+
<main role="main" class="pb-3">
34+
@RenderBody()
35+
</main>
36+
</div>
37+
38+
<footer class="border-top footer text-muted">
39+
<div class="container">
40+
&copy; 2021 - asp_net_core_filters - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
41+
</div>
42+
</footer>
43+
<script src="~/lib/jquery/dist/jquery.min.js"></script>
44+
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
45+
<script src="~/js/site.js" asp-append-version="true"></script>
46+
@await RenderSectionAsync("Scripts", required: false)
47+
</body>
48+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
2+
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@using asp_net_core_filters
2+
@using asp_net_core_filters.Models
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
Layout = "_Layout";
3+
}

0 commit comments

Comments
 (0)