当前位置:   article > 正文

C#桌面程序 winform WPF集成内置WebApi C# 创建HTTP Web API服务,winform WPF项目创建HTTP WEB服务, WPF WebApi 带权限访问 展示html

C#桌面程序 winform WPF集成内置WebApi C# 创建HTTP Web API服务,winform WPF项目创建HTTP WEB服务, WPF WebApi 带权限访问 展示html

      在维护旧的项目时,有时需要提供APP连接的需求,就要提供HTTP服务,winform项目就要提供HTTP服务,就不用再去写个c# web的IIS相关的业务了,简化项目的复杂度。

原先的业务get,post,请求返回文件功能全有

最新添加了权限认证的功能,业务接口只有获取正确的token后才能通信

1)验证通过后才能获取token

2)token可以设置定时过期

3)token里面可以绑定用户信息

4)展示html文件内容

最新资料下载------>   源代码资料

  1. //这里是部分代码,动态添加token,判断token是否存在,定时处理过期的token
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. namespace webAPIServer.Common
  7. {
  8. public class UnidDo
  9. {
  10. public static Dictionary<string, JObject> unidMap = new Dictionary<string, JObject>();
  11. private static readonly object unid_Lock = new object();
  12. public static string Add(string information) {
  13. string result = ""; ;
  14. try {
  15. lock (unid_Lock)
  16. {
  17. string key = Guid.NewGuid().ToString();
  18. if (!unidMap.ContainsKey(key))
  19. {
  20. string strDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
  21. JObject json = new JObject();
  22. json.Add(new JProperty("DateTime", strDate));
  23. json.Add(new JProperty("information", information));
  24. unidMap.Add(key, json);
  25. result = key;
  26. }
  27. else {
  28. result = Add(information);
  29. }
  30. }
  31. }
  32. catch (Exception ex)
  33. {
  34. Console.WriteLine("Add()--->" + ex.Message);
  35. return result;
  36. }
  37. return result;
  38. }
  39. public static void AutoDelKey()
  40. {
  41. try {
  42. while (true) {
  43. System.Threading.Thread.Sleep(1000*20);//每20秒遍历一次
  44. lock (unid_Lock)
  45. {
  46. List<string> list = new List<string>();
  47. foreach (KeyValuePair<string, JObject> kvp in unidMap)
  48. {
  49. JObject json = kvp.Value;
  50. DateTime oldDateTime = DateTime.Parse(json["DateTime"].ToString());
  51. if ((DateTime.Now- oldDateTime).TotalSeconds > 60*1) //当key的保存时间大于60秒就删除失效
  52. {
  53. list.Add(kvp.Key);
  54. }
  55. }
  56. for (int i = 0; i < list.Count; i++)
  57. {
  58. unidMap.Remove(list[i]);
  59. }
  60. }
  61. }
  62. }
  63. catch (Exception ex)
  64. {
  65. Console.WriteLine("AutoDelKey()--->" + ex.Message);
  66. }
  67. }
  68. public static bool unidContainsKey(string key) {
  69. bool result = false;
  70. try
  71. {
  72. lock (unid_Lock)
  73. {
  74. if (unidMap.ContainsKey(key))
  75. {
  76. JObject json = unidMap[key];
  77. json["DateTime"]= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
  78. unidMap[key] = json;
  79. result = true;
  80. }
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. Console.WriteLine("unidContainsKey()--->" + ex.Message);
  86. return result;
  87. }
  88. return result;
  89. }
  90. }
  91. }

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>展示漂亮网页</title>
  6. <link rel="stylesheet" href="/jscss/css/bootstrap.min.css">
  7. <link rel="stylesheet" href="/jscss/css/bootstrap-theme.min.css">
  8. <script src="/jscss/js/jquery-1.11.3.min.js"></script>
  9. <script src="/jscss/js/bootstrap.min.js"></script>
  10. </head>
  11. <body>
  12. <div class="page-header">
  13. <h1>Buttons</h1>
  14. </div>
  15. <p>
  16. <button type="button" class="btn btn-lg btn-default">Default</button>
  17. <button type="button" class="btn btn-lg btn-primary">Primary</button>
  18. <button type="button" class="btn btn-lg btn-success">Success</button>
  19. <button type="button" class="btn btn-lg btn-info">Info</button>
  20. <button type="button" class="btn btn-lg btn-warning">Warning</button>
  21. <button type="button" class="btn btn-lg btn-danger">Danger</button>
  22. <button type="button" class="btn btn-lg btn-link">Link</button>
  23. </p>
  24. <p>
  25. <button type="button" class="btn btn-default">Default</button>
  26. <button type="button" class="btn btn-primary">Primary</button>
  27. <button type="button" class="btn btn-success">Success</button>
  28. <button type="button" class="btn btn-info">Info</button>
  29. <button type="button" class="btn btn-warning">Warning</button>
  30. <button type="button" class="btn btn-danger">Danger</button>
  31. <button type="button" class="btn btn-link">Link</button>
  32. </p>
  33. <div class="page-header">
  34. <h1>Tables</h1>
  35. </div>
  36. <div class="row">
  37. <div class="col-md-6">
  38. <table class="table">
  39. <thead>
  40. <tr>
  41. <th>#</th>
  42. <th>First Name</th>
  43. <th>Last Name</th>
  44. <th>Username</th>
  45. </tr>
  46. </thead>
  47. <tbody>
  48. <tr>
  49. <td>1</td>
  50. <td>Mark</td>
  51. <td>Otto</td>
  52. <td>@mdo</td>
  53. </tr>
  54. <tr>
  55. <td>2</td>
  56. <td>Jacob</td>
  57. <td>Thornton</td>
  58. <td>@fat</td>
  59. </tr>
  60. <tr>
  61. <td>3</td>
  62. <td>Larry</td>
  63. <td>the Bird</td>
  64. <td>@twitter</td>
  65. </tr>
  66. </tbody>
  67. </table>
  68. </div>
  69. </div>
  70. <div class="page-header">
  71. <h1>Progress bars</h1>
  72. </div>
  73. <div class="progress">
  74. <div class="progress-bar progress-bar-striped" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%"><span class="sr-only">60% Complete</span></div>
  75. </div>
  76. <div class="progress">
  77. <div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100" style="width: 80%"><span class="sr-only">80% Complete (danger)</span></div>
  78. </div>
  79. <div class="progress">
  80. <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%;"><span class="sr-only">60% Complete</span></div>
  81. </div>
  82. <div class="page-header">
  83. <h1>Panels</h1>
  84. </div>
  85. <div class="row">
  86. <div class="col-sm-4">
  87. <div class="panel panel-default">
  88. <div class="panel-heading">
  89. <h3 class="panel-title">Panel title</h3>
  90. </div>
  91. <div class="panel-body">
  92. Panel content
  93. </div>
  94. </div>
  95. <div class="panel panel-primary">
  96. <div class="panel-heading">
  97. <h3 class="panel-title">Panel title</h3>
  98. </div>
  99. <div class="panel-body">
  100. Panel content
  101. </div>
  102. </div>
  103. </div><!-- /.col-sm-4 -->
  104. <div class="col-sm-4">
  105. <div class="panel panel-success">
  106. <div class="panel-heading">
  107. <h3 class="panel-title">Panel title</h3>
  108. </div>
  109. <div class="panel-body">
  110. Panel content
  111. </div>
  112. </div>
  113. <div class="panel panel-info">
  114. <div class="panel-heading">
  115. <h3 class="panel-title">Panel title</h3>
  116. </div>
  117. <div class="panel-body">
  118. Panel content
  119. </div>
  120. </div>
  121. </div><!-- /.col-sm-4 -->
  122. <div class="col-sm-4">
  123. <div class="panel panel-warning">
  124. <div class="panel-heading">
  125. <h3 class="panel-title">Panel title</h3>
  126. </div>
  127. <div class="panel-body">
  128. Panel content
  129. </div>
  130. </div>
  131. <div class="panel panel-danger">
  132. <div class="panel-heading">
  133. <h3 class="panel-title">Panel title</h3>
  134. </div>
  135. <div class="panel-body">
  136. Panel content
  137. </div>
  138. </div>
  139. </div><!-- /.col-sm-4 -->
  140. </div>
  141. </body>
  142. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/503249
推荐阅读
相关标签
  

闽ICP备14008679号