첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ AuditPolicyAttribute.cs

using System;

namespace TestProject
{
    /// <summary>
    /// 감사 정책 어트리뷰트
    /// </summary>
    public class AuditPolicyAttribute : Attribute
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 감사 필요 여부 - NeedsAudit

        /// <summary>
        /// 감사 필요 여부
        /// </summary>
        public bool NeedsAudit { get; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - AuditPolicyAttribute(needsAudit)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="needsAudit">감사 필요 여부</param>
        public AuditPolicyAttribute(bool needsAudit)
        {
            NeedsAudit = needsAudit;
        }

        #endregion
    }
}

 

728x90

 

▶ Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;

namespace TestProject
{
    /// <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(services)

        /// <summary>
        /// 서비스 컬렉션 구성하기
        /// </summary>
        /// <param name="services">서비스 컬렉션</param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
        }

        #endregion
        #region 구성하기 - Configure(app, environment)

        /// <summary>
        /// 구성하기
        /// </summary>
        /// <param name="app">애플리케이션 빌더</param>
        /// <param name="environment">웹 호스트 환경</param>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment environment)
        {
            if(environment.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // 위치 1 : 라우팅 실행 전. 라우팅이 실행되기 전에 요청에 영향을 미칠 수 있다.
            app.UseHttpMethodOverride();

            app.UseRouting();

            // 위치 2 : 라우팅 실행 후. 미들웨어는 메타 데이터를 기반으로 일치시킬 수 있다.
            app.Use
            (
                next => context =>
                {
                    Endpoint endpoint = context.GetEndpoint();

                    if(endpoint?.Metadata.GetMetadata<AuditPolicyAttribute>()?.NeedsAudit == true)
                    {
                        Console.WriteLine($"ACCESS TO SENSITIVE DATA AT : {DateTime.UtcNow}");
                    }

                    return next(context);
                }
            );

            app.UseEndpoints
            (
                endpoints =>
                {         
                    endpoints.MapGet
                    (
                        "/",
                        async context =>
                        {
                            await context.Response.WriteAsync("Hello world!");
                        }
                    );

                    // 메타 데이터를 사용하여 감사 정책을 구성한다.
                    endpoints.MapGet
                    (
                        "/sensitive",
                        async context =>
                        {
                            await context.Response.WriteAsync("sensitive data");
                        }
                    )
                    .WithMetadata(new AuditPolicyAttribute(needsAudit : true));
                }
            );
        } 

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요