상세 컨텐츠

본문 제목

신뢰할 수 있는 사이트 자동등록 하기

C#

by 탑~! 2013. 12. 26. 11:45

본문

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace CredentialManager
{
    public partial class Form1 : Form
    {
        public static Guid CLSID_InternetSecurityManager = new Guid("7b8a2d94-0ac9-11d1-896c-00c04fb6bfc4");
        public static Guid IID_IInternetSecurityManager = new Guid("79eac9ee-baf9-11ce-8c82-00aa004ba90b");

        private const uint SZM_CREATE = 0;
        private const uint SZM_DELETE = 1;

        public const uint ZoneLocalMachine = 0;
        public const uint ZoneIntranet = 1;
        public const uint ZoneTrusted = 2;
        public const uint ZoneInternet = 3;
        public const uint ZoneUntrusted = 4;

        public Form1()
        {
            InitializeComponent();
            AddSiteToZone(ZoneTrusted, "http://test.wonx.com");
        }

        private static IInternetSecurityManager CreateInternetSecurityManager()
        {
            Type iismType = Type.GetTypeFromCLSID(CLSID_InternetSecurityManager);
            return (IInternetSecurityManager)Activator.CreateInstance(iismType);
        }

        public static void AddSiteToZone(uint zone, string pattern)
        {
            try
            {
                IInternetSecurityManager ism = CreateInternetSecurityManager();
                ism.SetZoneMapping(zone, pattern, SZM_CREATE);
            }
            catch (COMException e)
            {
                throw new InvalidOperationException("URL has already been added to a zone", e);
            }
            catch (UnauthorizedAccessException e)
            {
                throw new InvalidOperationException("Can't add non-SSL site to zone that requires SSL", e);
            }
        }

        public static void RemoveSiteFromZone(string pattern)
        {
            uint currentZone;
            IInternetSecurityManager ism = CreateInternetSecurityManager();
            ism.MapUrlToZone(pattern, out currentZone, 0);
            ism.SetZoneMapping(currentZone, pattern, SZM_DELETE);
        }

        [ComImport, GuidAttribute("79EAC9EE-BAF9-11CE-8C82-00AA004BA90B"),
        InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IInternetSecurityManager
        {
            [return: MarshalAs(UnmanagedType.I4)]
            [PreserveSig]
            int SetSecuritySite([In] IntPtr pSite);

            [return: MarshalAs(UnmanagedType.I4)]
            [PreserveSig]
            int GetSecuritySite([Out] IntPtr pSite);

            [return: MarshalAs(UnmanagedType.I4)]
            [PreserveSig]
            int MapUrlToZone([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, out UInt32 pdwZone, UInt32 dwFlags);

            [return: MarshalAs(UnmanagedType.I4)]
            [PreserveSig]
            int GetSecurityId([MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, [MarshalAs(UnmanagedType.LPArray)] byte[] pbSecurityId, ref UInt32 pcbSecurityId, uint dwReserved);

            [return: MarshalAs(UnmanagedType.I4)]
            [PreserveSig]
            int ProcessUrlAction([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, UInt32 dwAction, out byte pPolicy, UInt32 cbPolicy, byte pContext, UInt32 cbContext, UInt32 dwFlags, UInt32 dwReserved);

            [return: MarshalAs(UnmanagedType.I4)]
            [PreserveSig]
            int QueryCustomPolicy([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, ref Guid guidKey, ref byte ppPolicy, ref UInt32 pcbPolicy, ref byte pContext, UInt32 cbContext, UInt32 dwReserved);

            [return: MarshalAs(UnmanagedType.I4)]
            [PreserveSig]
            int SetZoneMapping(UInt32 dwZone, [In, MarshalAs(UnmanagedType.LPWStr)] string lpszPattern, UInt32 dwFlags);

            [return: MarshalAs(UnmanagedType.I4)]
            [PreserveSig]
            int GetZoneMappings(UInt32 dwZone, out UCOMIEnumString ppenumString, UInt32 dwFlags);
        }
    }
}


관련글 더보기