zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using Microsoft.AspNetCore.Http;
using System;
using System.IO;
using System.Xml;
 
namespace Microsoft.AspNetCore.Builder
{
    /// <summary>
    ///
    /// </summary>
    public static class LicenseProviderExtensions
    {
        private const string LgbLicenseFileName = "Prow.lic";
        /// <summary>
        /// 增加CacheManager跨站点管理中间件
        /// </summary>
        /// <param name="builder"></param>
        public static IApplicationBuilder UseLicenseProvider(this IApplicationBuilder builder)
        {
            return builder.MapWhen(context => context.Request.Path == "/LicenseProvider.axd", app => app.Run(async context =>
            {
                var data = GetLicenseFromLicFile();
                await context.Response.WriteAsync(data ?? string.Empty);
            }));
        }
 
        private static string GetLicenseFromLicFile()
        {
            var ret = string.Empty;
            var licFile = Path.Combine(AppContext.BaseDirectory, LgbLicenseFileName);
            if (File.Exists(licFile))
            {
                var doc = new XmlDocument();
                try
                {
                    doc.Load(licFile);
                    ret = doc.ChildNodes[0].ChildNodes[0].InnerText;
                }
                catch { }
            }
            return ret;
        }
    }
}