ASP.NET

ASP.NET

Tags
Backend
C#
Published
Author

ASP.NET Core의 μ›Ή μ„œλ²„ κ΅¬ν˜„

Kestrel

κΈ°λ³Έ ν”Œλž«νΌ κ°„ HTTP μ„œλ²„ κ΅¬ν˜„
μ΅œμƒμ˜ μ„±λŠ₯κ³Ό λ©”λͺ¨λ¦¬ μ‚¬μš©λ₯ μ„ μ œκ³΅ν•˜μ§€λ§Œ HTTP.sys의 κ³ κΈ‰ κΈ°λŠ₯ 쀑 일뢀λ₯Ό μ œκ³΅ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
  • Windows, Linux 및 macOSμ—μ„œ μ‹€ν–‰λ˜λŠ” ν”Œλž«νΌ κ°„ μ›Ήμ„œλ²„
  • 인터넷을 ν¬ν•¨ν•œ λ„€νŠΈμ›Œν¬μ˜ μš”μ²­μ„ 직접 μ²˜λ¦¬ν•˜λŠ” 에지 μ„œλ²„λ‘œ 단독 μ‚¬μš©
notion image
  • IIS(인터넷 정보 μ„œλΉ„μŠ€), Nginx λ˜λŠ” Apache 같은 μ—­λ°©ν–₯ ν”„λ‘μ‹œ μ„œλ²„μ™€ 함꼐 μ‚¬μš©ν•©λ‹ˆλ‹€. μ—­λ°©ν–₯ ν”„λ‘μ‹œ μ„œλ²„λŠ” μΈν„°λ„·μ—μ„œ HTTP μš”μ²­μ„ λ°›μ•„ Kestrel에 전달
notion image

HTTP.sys

μœˆλ„μš° μ „μš© ASP.NET Core μ›Ή μ„œλ²„λ‘œ Http.sys, 컀널 λͺ¨λ“œ λ“œλΌμ΄λ²„μ— 기반
notion image
  • λ‚΄λΆ€ λ„€νŠΈμ›Œν¬μ—λ§Œ λ…ΈμΆœλ˜λŠ” 앱에도 μ‚¬μš© κ°€λŠ₯
notion image

ASP.NET Core의 κΆŒν•œ λΆ€μ—¬

κΆŒν•œ λΆ€μ—¬λŠ” μ‚¬μš©μžκ°€ ν•  수 μžˆλŠ” μž‘μ—…μ„ κ²°μ •ν•˜λŠ” ν”„λ‘œμ„ΈμŠ€

λ„€μž„μŠ€νŽ˜μ΄μŠ€

AuthorizeAttribute, AllowAnonymousAttribute
[Authorizae] public class AccountController : Controller { // μΈμ¦λ˜μ§€ μ•Šμ€ μ‚¬μš©μžμ˜ κ°œλ³„ μž‘μ—…μ— λŒ€ν•œ μ•‘μ„ΈμŠ€ ν—ˆμš© [AllowAnonymous] public ActionResult Login() {} public ActionResult Logout() {} }

μ—­ν•  기반 κΆŒν•œ λΆ€μ—¬

  • 선언적이며 μš”μ²­ν•œ λ¦¬μ†ŒμŠ€μ— μ•‘μ„ΈμŠ€ν•˜κΈ° μœ„ν•΄ ν˜„μž¬ μ‚¬μš©μžκ°€ 멀버 μžκ²©μ„ κ°€μ Έμ•Ό ν•˜λŠ” 역할을 지정
  • νŽ˜μ΄μ§€μ— λ°˜λ“œμ‹œ μ μš©λ˜μ–΄μ•Ό ν•˜λ©° Razor νŽ˜μ΄μ§€ 처리기 μˆ˜μ€€μ—μ„œλŠ” 적용될 수 μ—†μŠ΅λ‹ˆλ‹€.
[Authorize(Roles = "Administrator")] public class AdminstrationController : Controller { public IActionResult Index() => Content("Adminstrator"); }
μ—¬λŸ¬ 역할을 μ‰Όν‘œλ‘œ κ΅¬λΆ„λœ λͺ©λ‘μœΌλ‘œ 지정 κ°€λŠ₯
[Authorize(Roles = "HRManager,Finance")] public class SalaryController : Controller { public IActionResult Payslip() => Content("HRManager || Finance"); }

ν΄λ ˆμž„ 기반 κΆŒν•œ λΆ€μ—¬

  • 선언적
  • Razor νŽ˜μ΄μ§€, 컨트둀러, 컨트둀러 λ‚΄ μž‘μ—…μ— 적용
  • νŽ˜μ΄μ§€μ— λ°˜λ“œμ‹œ μ μš©λ˜μ–΄μ•Ό ν•˜λ©° Razor νŽ˜μ΄μ§€ 처리기 μˆ˜μ€€μ—λŠ” 적용될 수 μ—†λ‹€.
builder.Services.AddAuthorization(options => { options.AddPolicy("EmployeeOnly", policy => policy.RequireClaim("EmployeeNumber")); }); app.UseAuthorization();
정책을 λΉŒλ“œ 및 λ“±λ‘ν•˜κ³  UseAuthorization을 ν˜ΈμΆœν•©λ‹ˆλ‹€.
일반적으둜 Program.cs νŒŒμΌμ—μ„œ μˆ˜ν–‰ν•¨

μ •μ±… 기반 κΆŒν•œ λΆ€μ—¬

μš”κ΅¬μ‚¬ν•­, μš”κ΅¬μ‚¬ν•­ 처리기 및 미리 κ΅¬μ„±λœ 정책을 μ‚¬μš©
builder.Services.AddAuthorization(options => { options.AddPolicy("AtLeast21", policy => policy.Requirements.Add(new MinimumAgeRequirement(21))); }); // AtLeast21" 정책이 생성, μ΅œμ†Œ μ—°λ Ήμ΄λΌλŠ” ν•˜λ‚˜μ˜ μš”κ΅¬μ‚¬ν•­

IAuthorizationService

public interface IAuthorizationService { Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable<IAuthorizationRequirement> requirements); Task<AuthorizationResult> AuthorizeAsync( ClaimsPrincipal user, object resource, string policyName); } public interface IAuthorizationHandler { Task HandleAsync(AuthorizationHandlerContext context); }
  • IAuthorizationRequirementλŠ” λ©”μ„œλ“œκ°€ μ—†λŠ” ν‘œμ‹ μ„œλΉ„μŠ€μ΄λ©° κΆŒν•œ λΆ€μ—¬κ°€ μ„±κ³΅ν–ˆλŠ”μ§€ μ—¬λΆ€λ₯Ό μΆ”μ ν•˜κΈ° μœ„ν•œ λ©”μ»€λ‹ˆμ¦˜
  • IAuthorizationHandlerλŠ” μš”κ΅¬ 사항이 μΆ©μ‘±λ˜λŠ”μ§€ ν™•μΈν•˜λŠ” 역할을 ν•©λ‹ˆλ‹€.
Β