■ 커스텀 미들웨어를 사용해 API 키를 인증하는 방법을 보여준다.
[TestServer 프로젝트]
▶ Properteis/launchSettings.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
{ "$schema" : "http://json.schemastore.org/launchsettings.json", "iisSettings" : { "windowsAuthentication" : false, "anonymousAuthentication" : true, "iisExpress" : { "applicationUrl" : "http://localhost:41483", "sslPort" : 44362 } }, "profiles" : { "IIS Express" : { "commandName" : "IISExpress", "launchBrowser" : true, "launchUrl" : "swagger", "environmentVariables" : { "ASPNETCORE_ENVIRONMENT" : "Development" } }, "TestServer" : { "commandName" : "Project", "dotnetRunMessages" : "true", "launchBrowser" : true, "launchUrl" : "swagger", "applicationUrl" : "https://localhost:5001;http://localhost:5000", "environmentVariables" : { "ASPNETCORE_ENVIRONMENT" : "Development" } } } } |
▶ appsettings.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{ "Logging" : { "LogLevel" : { "Default" : "Information", "Microsoft" : "Warning", "Microsoft.Hosting.Lifetime" : "Information" } }, "AllowedHosts" : "*", "APIKey" : "A7A54AD3-91E1-4C3A-98C6-A17E39C9EA3D" } |
▶ APIKeyMiddleware.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System.Threading.Tasks; namespace TestServer { /// <summary> /// API 키 미들웨어 /// </summary> public class APIKeyMiddleware { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// API 키명 /// </summary> private static readonly string API_KEY_NAME = "APIKey"; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 다음 액션 /// </summary> private readonly RequestDelegate nextAction; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - APIKeyMiddleware(nextAction) /// <summary> /// 생성자 /// </summary> /// <param name="nextAction">다음 액션</param> public APIKeyMiddleware(RequestDelegate nextAction) { this.nextAction = nextAction; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 호출하기 (비동기) - InvokeAsync(context) /// <summary> /// 호출하기 (비동기) /// </summary> /// <param name="context">HTTP 컨텍스트</param> /// <returns>태스크</returns> public async Task InvokeAsync(HttpContext context) { if(!context.Request.Headers.TryGetValue(API_KEY_NAME, out var sourceAPIKey)) { context.Response.StatusCode = 401; await context.Response.WriteAsync("API Key was not provided."); return; } IConfiguration configuration = context.RequestServices.GetRequiredService<IConfiguration>(); string targetAPIKey = configuration.GetValue<string>(API_KEY_NAME); if(!targetAPIKey.Equals(sourceAPIKey)) { context.Response.StatusCode = 401; await context.Response.WriteAsync("Unauthorized client."); return; } await nextAction(context); } #endregion } } |
▶ WeatherForecast.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using System; namespace TestServer { /// <summary> /// 일기 예보 /// </summary> public class WeatherForecast { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 날짜 - Date /// <summary> /// 날짜 /// </summary> public DateTime Date { get; set; } #endregion #region 섭씨 온도 - TemperatureC /// <summary> /// 섭씨 온도 /// </summary> public int TemperatureC { get; set; } #endregion #region 화씨 온도 - TemperatureF /// <summary> /// 화씨 온도 /// </summary> public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); #endregion #region 요약 - Summary /// <summary> /// 요약 /// </summary> public string Summary { get; set; } #endregion } } |
▶ Controllers/WeatherForecastController.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; namespace TestServer.Controllers { /// <summary> /// 일기 예보 컨트롤러 /// </summary> [ApiController] [Route("[controller]")] [APIKey] public class WeatherForecastController : ControllerBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 로거 /// </summary> private readonly ILogger<WeatherForecastController> _logger; /// <summary> /// 요약 배열 /// </summary> private static readonly string[] _summaryArray = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - WeatherForecastController(logger) /// <summary> /// 생성자 /// </summary> /// <param name="logger">로거</param> public WeatherForecastController(ILogger<WeatherForecastController> logger) { _logger = logger; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 구하기 - Get() /// <summary> /// 구하기 /// </summary> /// <returns>일기 예보 열거 가능형</returns> [HttpGet] public IEnumerable<WeatherForecast> Get() { Random random = new Random(); return Enumerable.Range(1, 5).Select ( index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = random.Next(-20, 55), Summary = _summaryArray[random.Next(_summaryArray.Length)] } ) .ToArray(); } #endregion } } |
▶ Startup.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; namespace TestServer { /// <summary> /// 시작 /// </summary> public class Startup { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 구성 - Configuration /// <summary> /// 구성 /// </summary> public IConfiguration Configuration { get; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Startup(configuration) /// <summary> /// 생성자 /// </summary> /// <param name="configuration">구성</param> public Startup(IConfiguration configuration) { Configuration = configuration; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 서비스 컬렉션 구성하기 - ConfigureServices(serviceCollection) /// <summary> /// 서비스 컬렉션 구성하기 /// </summary> /// <param name="serviceCollection">서비스 컬렉션</param> public void ConfigureServices(IServiceCollection serviceCollection) { serviceCollection.AddControllers(); serviceCollection.AddSwaggerGen ( options => { options.SwaggerDoc("v1", new OpenApiInfo { Title = "TestServer", Version = "v1" }); } ); } #endregion #region 구성하기 - Configure(applicationBuilder, webHostEnvironment) /// <summary> /// 구성하기 /// </summary> /// <param name="applicationBuilder">애플리케이션 빌더</param> /// <param name="webHostEnvironment">웹 호스트 환경</param> public void Configure(IApplicationBuilder applicationBuilder, IWebHostEnvironment webHostEnvironment) { if(webHostEnvironment.IsDevelopment()) { applicationBuilder.UseDeveloperExceptionPage(); applicationBuilder.UseSwagger(); applicationBuilder.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestServer v1")); } applicationBuilder.UseHttpsRedirection(); applicationBuilder.UseRouting(); applicationBuilder.UseAuthorization(); applicationBuilder.UseMiddleware<APIKeyMiddleware>(); applicationBuilder.UseEndpoints ( endpointRouteBuilder => { endpointRouteBuilder.MapControllers(); } ); } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; namespace TestServer { /// <summary> /// 프로그램 /// </summary> public class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 호스트 빌더 생성하기 - CreateHostBuilder(argumentList) /// <summary> /// 호스트 빌더 생성하기 /// </summary> /// <param name="argumentList">인자 리스트</param> /// <returns>호스트 빌더</returns> public static IHostBuilder CreateHostBuilder(string[] argumentList) => Host.CreateDefaultBuilder(argumentList) .ConfigureWebHostDefaults ( webBuilder => { webBuilder.UseStartup<Startup>(); } ); #endregion #region 프로그램 시작하기 - Main(argumentList) /// <summary> /// 프로그램 시작하기 /// </summary> /// <param name="argumentList">인자 리스트</param> public static void Main(string[] argumentList) { CreateHostBuilder(argumentList).Build().Run(); } #endregion } } |
[TestClient 프로젝트]
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; namespace TestClient { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region HTTP 클라이언트 구하기 - GetHTTPClient(baseAddress) /// <summary> /// HTTP 클라이언트 구하기 /// </summary> /// <param name="baseAddress">기준 주소</param> /// <returns>HTTP 클라이언트</returns> private static HttpClient GetHTTPClient(string baseAddress) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(baseAddress); client.DefaultRequestHeaders.Add("APIKey", "A7A54AD3-91E1-4C3A-98C6-A17E39C9EA3D"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); return client; } #endregion #region 리스트 구하기 - GetList(client) /// <summary> /// 리스트 구하기 /// </summary> /// <param name="client">HTTP 클라이언트</param> /// <returns>리스트</returns> private static async Task<List<WeatherForecast>> GetList(HttpClient client) { string url = string.Format("/WeatherForecast"); HttpResponseMessage response = await client.GetAsync(url); if(response.IsSuccessStatusCode) { List<WeatherForecast> list = await response.Content.ReadAsAsync<List<WeatherForecast>>(); return list; } else { Console.WriteLine(response.StatusCode); Console.WriteLine(await response.Content.ReadAsStringAsync()); } return null; } #endregion #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> static void Main() { using(HttpClient client = GetHTTPClient("https://localhost:44362")) { List<WeatherForecast> list = GetList(client).GetAwaiter().GetResult(); for(int i = 0; i < list.Count; i++) { Console.WriteLine(list[i].Summary); } } Console.WriteLine("--------------------------------------------------"); Console.WriteLine("아무 키나 눌러 주시기 바랍니다."); Console.ReadKey(true); } #endregion } } |