프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / License.Validator / Validator.cs @ cf1cc862

이력 | 보기 | 이력해설 | 다운로드 (5.66 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Linq;
5
using System.Management;
6
using System.ServiceModel;
7
using System.ServiceModel.Channels;
8
using System.Text;
9
using System.Threading.Tasks;
10
using System.Windows;
11
using System.Windows.Resources;
12
using System.Xml;
13
using System.Xml.Serialization;
14

    
15
namespace License.Validator
16
{
17
    public class Valid
18
    {
19
        public event EventHandler<ValidateErrorArgs> ValidateError;
20

    
21
        license.iLicenseService service;
22

    
23
        private string key;
24
      
25
        private string _serviceUri;
26
        private CustomBinding binding;
27
        private string license;
28

    
29
        public string Name;
30

    
31
        public Valid(string serviceUri)
32
        {
33
            _serviceUri = $"{serviceUri}/LicenseService.svc";
34

    
35
            binding = new CustomBinding();
36
            SymmetricSecurityBindingElement ssbe = SecurityBindingElement.CreateSspiNegotiationBindingElement(true);
37
            ssbe.LocalClientSettings.MaxClockSkew = new TimeSpan(0, 10, 0);
38
            ssbe.LocalServiceSettings.MaxClockSkew = new TimeSpan(0, 10, 0);
39
            binding.Elements.Add(ssbe);
40
            binding.Elements.Add(new TextMessageEncodingBindingElement());
41
            binding.Elements.Add(new HttpTransportBindingElement());
42
        }
43

    
44
        public void GetLicense(string keyfile)
45
        {
46
            try
47
            {
48
                service = ChannelFactory<license.iLicenseService>.CreateChannel(binding, new EndpointAddress(_serviceUri));
49
            
50
                Uri uri = new Uri($"/{keyfile}", UriKind.Relative);
51
                StreamResourceInfo info = Application.GetResourceStream(uri);
52
                System.IO.StreamReader stream = new System.IO.StreamReader(info.Stream);
53
                key = stream.ReadToEnd();
54

    
55
                license = service.GetLicense(key, Environment.MachineName,ProcessID(), Environment.UserName);
56

    
57
                var doc = new XmlDocument();
58
                doc.LoadXml(license);
59

    
60
                var element = doc.GetElementsByTagName("name");
61

    
62
                if(element != null)
63
                {
64
                    this.Name = element[0].InnerText;
65
                }
66
            }
67
            catch (ProtocolException)
68
            {
69
                Error(new ValidateErrorArgs("License Connect Error", new Exception("")));
70
            }
71
            catch(Exception ex)
72
            { 
73
                Error(new ValidateErrorArgs("Validate Error 001", ex));
74
            }
75
            finally
76
            {
77
                var communicationObject = service as ICommunicationObject;
78
                if (communicationObject != null)
79
                {
80
                    try
81
                    {
82
                        communicationObject.Close(TimeSpan.FromMilliseconds(50));
83
                    }
84
                    catch
85
                    {
86
                        communicationObject.Abort();
87
                    }
88
                }
89
            }
90
        }
91

    
92
        private string ProcessID()
93
        {
94
            return string.Join("", GetMainBoardSerialNumberList()) + string.Join("", GetCPUIDList());
95
        }
96

    
97
        private List<string> GetMainBoardSerialNumberList()
98
        {
99
            List<string> list = new List<string>();
100

    
101
            string query = "SELECT * FROM Win32_BaseBoard";
102

    
103
            ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(query);
104

    
105
            foreach (ManagementObject managementObject in managementObjectSearcher.Get())
106
            {
107
                list.Add(managementObject.GetPropertyValue("SerialNumber").ToString());
108
            }
109

    
110
            return list;
111
        }
112
        
113
        private List<string> GetCPUIDList()
114
        {
115
            List<string> list = new List<string>();
116

    
117
            string query = "Select * FROM Win32_Processor";
118

    
119
            ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(query);
120

    
121
            foreach (ManagementObject managementObject in managementObjectSearcher.Get())
122
            {
123
                list.Add(managementObject.GetPropertyValue("ProcessorId").ToString());
124
            }
125

    
126
            return list;
127
        }
128

    
129

    
130
        public bool Activate()
131
        {
132
            try
133
            {
134
                service = ChannelFactory<license.iLicenseService>.CreateChannel(binding, new EndpointAddress(_serviceUri));
135
                
136
                if(!service.Validate(key, license))
137
                {
138
                    Error(new ValidateErrorArgs("Validate Error 002", new Exception("null")));
139
                }
140
                else
141
                {
142
                    return true;
143
                }
144
            }
145
            catch (Exception)
146
            {
147
                Error(new ValidateErrorArgs("Validate Error 003", new Exception("null")));
148
            }
149
            finally
150
            {
151
                var communicationObject = service as ICommunicationObject;
152
                if (communicationObject != null)
153
                {
154
                    try
155
                    {
156
                        communicationObject.Close(TimeSpan.FromMilliseconds(50));
157
                    }
158
                    catch
159
                    {
160
                        communicationObject.Abort();
161
                    }
162
                }
163
            }
164

    
165
            return false;
166
        }
167

    
168
        private void Error(ValidateErrorArgs error)
169
        {
170
            bool flag = false;
171

    
172
            if (ValidateError != null)
173
            {
174
                ValidateError(null, error);
175

    
176
                if (error.Received)
177
                    flag = true;
178
            }
179

    
180
            if (!flag)
181
            {
182
                MessageBox.Show(error.Message);
183
                Environment.Exit(0);
184
            }
185
        }
186
    }
187
}
클립보드 이미지 추가 (최대 크기: 500 MB)