IISRedirectToHTTPS.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Windows.Forms;
  3. using Microsoft.Web.Administration;
  4. namespace IISRedirectToHTTPS
  5. {
  6. public partial class IISRedirectToHTTPS : Form
  7. {
  8. private const string RULE_NAME = "Redirect to HTTPS";
  9. public IISRedirectToHTTPS()
  10. {
  11. InitializeComponent();
  12. foreach (Site site in new ServerManager().Sites)
  13. {
  14. cb_Site.Items.Add(site.Name);
  15. }
  16. cb_Site.SelectedIndex = 0;
  17. }
  18. private void btn_Registry_Click(object sender, EventArgs e)
  19. {
  20. // IIS 서버 관리자 객체 생성
  21. ServerManager serverManager = new ServerManager();
  22. // 대상 사이트 가져오기
  23. Site site = serverManager.Sites[cb_Site.SelectedItem.ToString()];
  24. // 사이트의 재작성 섹션 가져오기
  25. Configuration config = site.GetWebConfiguration();
  26. ConfigurationSection rewriteSection = config.GetSection("system.webServer/rewrite/rules");
  27. // 새 규칙 생성
  28. ConfigurationElementCollection rulesCollection = rewriteSection.GetCollection();
  29. if (ExistRule(rulesCollection))
  30. {
  31. MessageBox.Show("이미 URL 재작성 규칙이 존재합니다.");
  32. return;
  33. }
  34. else
  35. {
  36. // 규칙을 규칙 컬렉션에 추가
  37. rulesCollection.Add(CreateNewRule(rulesCollection));
  38. // 변경 사항 저장
  39. serverManager.CommitChanges();
  40. MessageBox.Show("URL 재작성 규칙이 추가되었습니다.");
  41. }
  42. }
  43. private bool ExistRule(ConfigurationElementCollection rulesCollection)
  44. {
  45. foreach (ConfigurationElement existingRule in rulesCollection)
  46. {
  47. if (existingRule["name"].ToString() == RULE_NAME)
  48. return true;
  49. }
  50. return false;
  51. }
  52. private ConfigurationElement CreateNewRule(ConfigurationElementCollection rulesCollection)
  53. {
  54. ConfigurationElement rule = rulesCollection.CreateElement("rule");
  55. rule["name"] = RULE_NAME;
  56. rule["patternSyntax"] = "ECMAScript";
  57. rule["stopProcessing"] = true;
  58. // 일치 조건 설정
  59. ConfigurationElement match = rule.GetChildElement("match");
  60. match["url"] = "(.*)";
  61. // 조건 그룹 설정
  62. ConfigurationElement conditions = rule.GetChildElement("conditions");
  63. conditions["logicalGrouping"] = "MatchAll";
  64. conditions["trackAllCaptures"] = false;
  65. // 조건 추가
  66. ConfigurationElementCollection conditionsCollection = conditions.GetCollection();
  67. ConfigurationElement condition = conditionsCollection.CreateElement("add");
  68. condition["input"] = "{HTTPS}";
  69. condition["pattern"] = "^OFF$";
  70. condition["ignoreCase"] = true; // 대/소문자 무시
  71. conditionsCollection.Add(condition);
  72. // 동작 설정
  73. ConfigurationElement action = rule.GetChildElement("action");
  74. action["type"] = "Redirect";
  75. action["url"] = $"https://{"{SERVER_NAME}"}:{nud_HttpsPort.Value}/{"{R:1}"}";
  76. action["appendQueryString"] = true;
  77. action["redirectType"] = "SeeOther";
  78. return rule;
  79. }
  80. }
  81. }