-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
XuanThuLab
committed
Aug 27, 2020
1 parent
a4a4257
commit e984ff3
Showing
13 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
// Use IntelliSense to find out which attributes exist for C# debugging | ||
// Use hover for the description of the existing attributes | ||
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": ".NET Core Launch (web)", | ||
"type": "coreclr", | ||
"request": "launch", | ||
"preLaunchTask": "build", | ||
// If you have changed target frameworks, make sure to update the program path. | ||
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/02.middleware.dll", | ||
"args": [], | ||
"cwd": "${workspaceFolder}", | ||
"stopAtEntry": false, | ||
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser | ||
"serverReadyAction": { | ||
"action": "openExternally", | ||
"pattern": "\\bNow listening on:\\s+(https?://\\S+)" | ||
}, | ||
"env": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"sourceFileMap": { | ||
"/Views": "${workspaceFolder}/Views" | ||
} | ||
}, | ||
{ | ||
"name": ".NET Core Attach", | ||
"type": "coreclr", | ||
"request": "attach", | ||
"processId": "${command:pickProcess}" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "build", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"build", | ||
"${workspaceFolder}/02.middleware.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "publish", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"publish", | ||
"${workspaceFolder}/02.middleware.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "watch", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"watch", | ||
"run", | ||
"${workspaceFolder}/02.middleware.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<RootNamespace>_02.middleware</RootNamespace> | ||
</PropertyGroup> | ||
|
||
</Project> |
43 changes: 43 additions & 0 deletions
43
ASP_NET_CORE/02.middleware/Middleware/CheckAcessMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace _02.middleware { | ||
public class CheckAcessMiddleware { | ||
// Lưu middlewware tiếp theo trong Pipeline | ||
private readonly RequestDelegate _next; | ||
public CheckAcessMiddleware (RequestDelegate next) => _next = next; | ||
public async Task Invoke (HttpContext httpContext) { | ||
if (httpContext.Request.Path == "/testxxx") { | ||
|
||
Console.WriteLine ("CheckAcessMiddleware: Cấm truy cập"); | ||
await Task.Run ( | ||
async () => { | ||
string html = "<h1>CAM KHONG DUOC TRUY CAP</h1>"; | ||
httpContext.Response.StatusCode = StatusCodes.Status403Forbidden; | ||
await httpContext.Response.WriteAsync (html); | ||
} | ||
); | ||
|
||
} else { | ||
|
||
// Thiết lập Header cho HttpResponse | ||
httpContext.Response.Headers.Add ("throughCheckAcessMiddleware", new [] { DateTime.Now.ToString () }); | ||
|
||
Console.WriteLine ("CheckAcessMiddleware: Cho truy cập"); | ||
|
||
// Chuyển Middleware tiếp theo trong pipeline | ||
await _next (httpContext); | ||
|
||
} | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace _02.middleware { | ||
public class FrontMiddleware : IMiddleware | ||
{ | ||
public async Task InvokeAsync(HttpContext context, RequestDelegate next) | ||
{ | ||
Console.Clear(); | ||
Console.WriteLine("FrontMiddleware: " + context.Request.Path); | ||
context.Items.Add("dulieu1", "Data Object ..."); | ||
|
||
await next(context); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
namespace _02.middleware | ||
{ | ||
public static class MyAppExtensions | ||
{ | ||
// Mở rộng cho IApplicationBuilder phương thức UseCheckAccess | ||
public static IApplicationBuilder UseCheckAccess(this IApplicationBuilder builder) | ||
{ | ||
return builder.UseMiddleware<CheckAcessMiddleware>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace _02.middleware | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:29289", | ||
"sslPort": 44349 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"_02.middleware": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"applicationUrl": "https://localhost:5001;http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace _02.middleware | ||
{ | ||
public class Startup | ||
{ | ||
|
||
// Đăng ký các dịch vụ sử dụng bởi ứng dụng, services là một DI container | ||
// Xem: https://xuanthulab.net/dependency-injection-di-trong-c-voi-servicecollection.html | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddTransient<FrontMiddleware>(); | ||
services.AddDistributedMemoryCache(); // Thêm dịch vụ dùng bộ nhớ lưu cache (session sử dụng dịch vụ này) | ||
services.AddSession(); // Thêm dịch vụ Session, dịch vụ này cunng cấp Middleware: SessionMiddleware | ||
} | ||
|
||
|
||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
{ | ||
|
||
app.UseAuthentication | ||
|
||
app.UseMiddleware<FrontMiddleware>(); | ||
|
||
|
||
// Thêm StaticFileMiddleware - nếu Request là yêu cầu truy cập file tĩnh, | ||
// Nó trả ngay về Response nội dung file và là điểm cuối pipeline, nếu khác | ||
// nó gọi Middleware phía sau trong Pipeline | ||
app.UseStaticFiles(); | ||
|
||
|
||
// Thêm SessionMiddleware: khôi phục, thiết lập - tạo ra session | ||
// gán context.Session, sau đó chuyển gọi ngay middleware | ||
// tiếp trong pipeline | ||
app.UseSession(); | ||
|
||
// Thêm middleware - CheckAccessMiddleware | ||
app.UseCheckAccess(); | ||
|
||
|
||
// Thêm EndpointRoutingMiddleware: ánh xạ Request gọi đến Endpoint (Middleware cuối) | ||
// phù hợp định nghĩa bởi EndpointMiddleware | ||
app.UseRouting(); | ||
|
||
// app.UseEndpoint dùng để xây dựng các endpoint - điểm cuối của pipeline theo Url truy cập | ||
app.UseEndpoints(endpoints => | ||
{ | ||
// EndPoint(2) khi truy vấn đến /Testpost với phương thức post hoặc put | ||
endpoints.MapMethods("/Testpost" , new string[] {"post", "put"}, async context => { | ||
context.Session.SetInt32("count", 1); | ||
await context.Response.WriteAsync("post/pust"); | ||
}); | ||
// EndPoint(2) - Middleware khi truy cập /Home với phương thức GET - nó làm Middleware cuối Pipeline | ||
endpoints.MapGet("/Home", async context => { | ||
int? count = context.Session.GetInt32("count"); | ||
count = (count != null) ? count + 1 : 1; | ||
context.Session.SetInt32("count", count.Value); | ||
await context.Response.WriteAsync($"Home page! {count}"); | ||
}); | ||
}); | ||
|
||
// EndPoint(3) app.Run tham số là hàm delegate tham số là HttpContex | ||
// - nó tạo điểm cuối của pipeline. | ||
app.Run(async context => { | ||
context.Response.StatusCode = StatusCodes.Status404NotFound; | ||
context.Session.SetInt32("count", 1); | ||
await context.Response.WriteAsync("Page not found"); | ||
}); | ||
|
||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# (ASP.NET Core) Tạo middleware và đăng ký vào pipeline của ứng dụng Web C# CSharp | ||
|
||
https://xuanthulab.net/asp-net-core-tao-middleware-va-dang-ky-vao-pipeline-cua-ung-dung-web-c-csharp.html#transferdata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Static file |