■ 응용 프로그램 구성 암호화 하기 예제

----------------------------------------------------------------------------------------------------

ConfigurationHelper.SetProtection("connectionStrings", ConfigurationHelper.ActionType.Enabled);

----------------------------------------------------------------------------------------------------

 

■ 응용 프로그램 구성 암호화 하기

----------------------------------------------------------------------------------------------------

using System.Configuration;

 

/// <summary>

/// 구성 헬퍼

/// </summary>

public static class ConfigurationHelper

{

    //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region 액션 타입 - ActionType

 

    /// <summary>

    /// 액션 타입

    /// </summary>

    public enum ActionType

    {

        /// <summary>

        /// 이용 가능

        /// </summary>

        Enabled,

            

        /// <summary>

        /// 이용 불가

        /// </summary>

        Disabled

    }

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method

    ////////////////////////////////////////////////////////////////////////////////////////// Static

    //////////////////////////////////////////////////////////////////////////////// Public

 

    #region 보호 설정하기 - SetProtection(section, actionType)

 

    /// <summary>

    /// 보호 설정하기

    /// </summary>

    /// <param name="section">섹션</param>

    /// <param name="actionType">액션 타입</param>

    public static void SetProtection(string section, ActionType actionType)

    {

        Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);


        ConfigurationSection configurationSection = configuration.GetSection(section);

 

        if(configurationSection != null && !configurationSection.IsReadOnly())

        {

            if(actionType == ActionType.Enabled)

            {

                Protect(configuration, configurationSection);

            }

            else if(actionType == ActionType.Disabled)

            {

                Unprotect(configuration, configurationSection);

            }

        }

    }

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////// Private

 

    #region 보호하기 - Protect(configuration, configurationSection)

 

    /// <summary>

    /// 보호하기

    /// </summary>

    /// <param name="configuration">구성</param>

    /// <param name="configurationSection">구성 섹션</param>

    private static void Protect(Configuration configuration, ConfigurationSection configurationSection)

    {

        if(!configurationSection.SectionInformation.IsProtected)

        {

            configurationSection.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");

 

            configurationSection.SectionInformation.ForceSave = true;

 

            configuration.Save(ConfigurationSaveMode.Full);

        }

    }

 

    #endregion

    #region 보호 취소하기 - Unprotect(configuration, configurationSection)

 

    /// <summary>

    /// 보호 취소하기

    /// </summary>

    /// <param name="configuration">구성</param>

    /// <param name="configurationSection">구성 섹션</param>

    private static void Unprotect(Configuration configuration, ConfigurationSection configurationSection)

    {

        if(configurationSection.SectionInformation.IsProtected)

        {

            configurationSection.SectionInformation.UnprotectSection();

 

            configurationSection.SectionInformation.ForceSave = true;

 

            configuration.Save(ConfigurationSaveMode.Full);

        }

    }

 

    #endregion

}

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요