source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet.WinForm/Forms/VerifyCodeUpdateDialog.cs@ 1146

Last change on this file since 1146 was 1146, checked in by Sam Habiel, 13 years ago

Initial Import of BMX4

File size: 18.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using IndianHealthService.BMXNet.WinForm.Forms;
9using IndianHealthService.BMXNet.Net;
10using IndianHealthService.BMXNet.WinForm.Configuration;
11using System.Security.Cryptography;
12using System.Security.Permissions;
13using System.Security.Principal;
14using System.Net.Sockets;
15using System.Net;
16using System.Threading;
17
18namespace IndianHealthService.BMXNet.WinForm.Forms
19{
20 public partial class VerifyCodeUpdateDialog : Form
21 {
22 /// <summary>
23 /// Private variable that takes framework as parameter
24 /// </summary>
25 private WinFramework _framework = null;
26
27 /// <summary>
28 /// Login process, not sure if this is needed
29 /// </summary>
30 private LoginProcess mylogin;
31
32 private RpmsConnectionSpec _connectionSpec = null;
33
34 /// <summary>
35 /// The active ConnectionSpec being used to login. With care, the property can be changed or the instance can be modified during the AttemptingLogin and LoginAttempted events
36 /// with care.
37 /// </summary>
38 public RpmsConnectionSpec ConnectionSpec
39 {
40 get { return _connectionSpec; }
41 set { _connectionSpec = value; }
42 }
43 private string _accessCode;
44
45 internal protected WinFramework Framework
46 {
47 get { return _framework; }
48 set { _framework = value; }
49 }
50
51 private bool isUpdateSuccessful;
52
53 public VerifyCodeUpdateDialog(WinFramework framework, RpmsConnectionSpec connspec, string accesscode)
54 {
55 this.Framework = framework;
56 this._accessCode = accesscode;
57 this.ConnectionSpec = connspec;
58 mylogin = new LoginProcess(framework);
59
60 InitializeComponent();
61 }
62
63 private bool _isSwitchServerModeEnabled = false;
64
65 public bool IsSwitchServerModeEnabled
66 {
67 get { return _isSwitchServerModeEnabled; }
68 set { _isSwitchServerModeEnabled = value; }
69 }
70
71 private void VerifyCodeUpdateDialog_Load(object sender, EventArgs e)
72 {
73 this.currentVerifyCodeEntry.Focus();
74 }
75
76 public void ShowView(IWin32Window anOwner)
77 {
78 this.ShowDialog(anOwner);
79 }
80
81 public VerifyCodeUpdateResult ShowVerifyChangeDialog(IWin32Window anOwner)
82 {
83 DialogResult dr = new DialogResult();
84 if (anOwner != null)
85 {
86 dr = this.ShowDialog(anOwner);
87 }
88 else
89 {
90 dr = this.ShowDialog();
91 }
92
93 VerifyCodeUpdateResult vr = new VerifyCodeUpdateResult();
94 vr.DialogResult = dr;
95 vr.WasVerifyCodeUpdatedSuccessfully = this.IsUpdateSuccessful;
96
97 return vr;
98 }
99
100 public IWin32Window UiOwner
101 {
102 get { return this.Owner; }
103 }
104
105
106 #region RpmsLoginView Members
107
108 public string Title
109 {
110 set { this.Text = value; }
111 }
112
113 public bool IsUpdateSuccessful
114 {
115 set { this.isUpdateSuccessful = value; }
116 get { return this.isUpdateSuccessful; }
117 }
118
119 public string CurrentVerifyCode
120 {
121 get { return this.currentVerifyCodeEntry.Text; }
122 }
123
124 public string AccessCode
125 {
126 set { this._accessCode = value; }
127 get { return this._accessCode; }
128 }
129
130 private string _updateMessage;
131
132 public string UpdateMessage
133 {
134 set { this._updateMessage = value; }
135 get { return this._updateMessage; }
136 }
137 public string NewVerifyCode
138 {
139 get { return this.NewVerifyCodeEntry.Text; }
140 }
141
142 public string RepeatNewVerifyCode
143 {
144 get { return this.RepeatVerifyCodeEntry.Text; }
145 }
146
147
148 public event EventHandler Ok;
149
150 public event EventHandler Cancel;
151
152 #endregion
153
154
155 public void CloseView()
156 {
157 this.Close();
158 }
159
160 private void okButton_Click(object sender, EventArgs e)
161 {
162 //make sure all fields were filled with values
163 if ((this.currentVerifyCodeEntry.Text.Length == 0) ||
164 (this.NewVerifyCodeEntry.Text.Length == 0) ||
165 (this.RepeatVerifyCodeEntry.Text.Length == 0) )
166 {
167 string message = "Please fill all blank fields";
168 string caption = "Verify Code Update";
169 MessageBoxButtons buttons = MessageBoxButtons.OK;
170
171 DialogResult result;
172
173 // Displays the MessageBox.
174 result = MessageBox.Show(message, caption, buttons);
175 }
176 else if (this.currentVerifyCodeEntry.Text.Contains("^") ||
177 this.NewVerifyCodeEntry.Text.Contains("^") ||
178 this.RepeatVerifyCodeEntry.Text.Contains("^"))
179 {
180 string message = "The character '^' is not allowed in any of the verify code entry fields.";
181 string caption = "Verify Code Update";
182 MessageBoxButtons buttons = MessageBoxButtons.OK;
183
184 DialogResult result;
185
186 //Display the MessageBox
187 result = MessageBox.Show(message, caption, buttons);
188
189 ResetVerifyCodeEntries();
190 }
191 else if (this.NewVerifyCodeEntry.Text != this.RepeatVerifyCodeEntry.Text)
192 {
193 string message = "Please key in the same verify code twice.";
194 string caption = "Verify Code Update";
195 MessageBoxButtons buttons = MessageBoxButtons.OK;
196
197 DialogResult result;
198
199 // Displays the MessageBox.
200 result = MessageBox.Show(message, caption, buttons);
201 }
202 else
203 {
204 //get all the values and ready to udpate
205 string currentVCode = this.currentVerifyCodeEntry.Text;
206 string newVCode = this.NewVerifyCodeEntry.Text;
207 string repeatVCode = this.RepeatVerifyCodeEntry.Text;
208
209 this.IsUpdateSuccessful = SendSecurityRequest(EncryptAccessVerifyCode(this.AccessCode,currentVCode,newVCode));
210
211 if (!this.IsUpdateSuccessful)
212 {
213 ResetVerifyCodeEntries();
214 }
215 }
216 }
217
218 private void cancelButton_Click(object sender, EventArgs e)
219 {
220 if (this.Cancel != null)
221 {
222 this.Cancel(this, new EventArgs());
223 }
224 }
225
226 public void ResetVerifyCodeEntries()
227 {
228 this.currentVerifyCodeEntry.Clear();
229 this.RepeatVerifyCodeEntry.Clear();
230 this.NewVerifyCodeEntry.Clear();
231 this.currentVerifyCodeEntry.Select();
232 }
233
234 public void ShowWait()
235 {
236 this.Cursor = Cursors.WaitCursor;
237 }
238
239 public void HideWait()
240 {
241 this.Cursor = Cursors.Default;
242 }
243 public String EncryptAccessVerifyCode(string anAccessCode, string aCurrentVerifyCode, string aNewVerifyCode)
244 {
245 BMXNetBroker broker = this.Framework.SocketBroker;
246 string accessVerifyPair = anAccessCode.ToUpper() + ";" + aNewVerifyCode.ToUpper() + ";" + aCurrentVerifyCode.ToUpper();
247 return broker.EncryptionProvider.Encrypt(accessVerifyPair);
248 }
249
250 private bool SendSecurityRequest(string encryptedAccessVerifyCode)
251 {
252 BMXNetSocketBroker broker = (BMXNetSocketBroker)this.Framework.SocketBroker;
253
254 string strReceive = "";
255 string cMSG;
256
257 string m_sWKID = "BMX";
258 string m_sWINH = "";
259 string m_sPRCH = "";
260 string m_sWISH = "";
261 string m_cHDR = broker.ADEBHDR(m_sWKID,m_sWINH,m_sPRCH,m_sWISH);
262
263 //open a TCP sockry
264 TcpClient mySocket = this.OpenConnectionCommon();
265
266 cMSG = broker.ADEBLDMsg(m_cHDR, "BMX CVC", encryptedAccessVerifyCode);
267 SendString(mySocket, cMSG, "");
268
269 strReceive = ReceiveString(mySocket);
270
271 //"2VERIFY CODE must be a mix of alpha and numerics."
272 //"3This code is the same as the current one."
273 //"4This has been used previously as the VERIFY CODE."
274 //"5VERIFY CODE must be different than the ACCESS CODE."
275 List<string> rspMessages = new List<string>(); // List of received messages
276 rspMessages.Add("Sorry that isn't the correct current code");
277 rspMessages.Add("Enter 8-20 characters mixed alphanumeric and punctuation (except '^', ';', ':').");
278 rspMessages.Add("VERIFY CODE must be a mix of alpha and numerics and punctuation."); // String element 2
279 rspMessages.Add("This code is the same as the current one."); // 3
280 rspMessages.Add("This has been used previously as the VERIFY CODE."); // 4
281 rspMessages.Add("VERIFY CODE must be different than the ACCESS CODE."); // 5
282
283 if (strReceive.StartsWith("M ERROR="))
284 {
285 string caption = "Verify Code Update";
286 MessageBoxButtons buttons = MessageBoxButtons.OK;
287
288 DialogResult result;
289
290 // Displays the MessageBox.
291 result = MessageBox.Show(strReceive, caption, buttons);
292 return false;
293 }
294 else
295 {
296 foreach (string errorMsg in rspMessages)
297 {
298 if (strReceive.Contains(errorMsg))
299 {
300 this.UpdateMessage = errorMsg;
301 string caption = "Verify Code Update";
302 MessageBoxButtons buttons = MessageBoxButtons.OK;
303
304 DialogResult result;
305
306 // Displays the MessageBox.
307 result = MessageBox.Show(errorMsg, caption, buttons);
308 return false;
309 }
310 }
311
312 //if the code runs here, it means the update is successful
313 if (strReceive.Contains("0"))
314 {
315 string message = "Verify code has been successfully updated. ";
316 MessageBoxButtons msgButtons = MessageBoxButtons.OK;
317
318 // Displays the MessageBox.
319 MessageBox.Show(message, "Verify Code Update", msgButtons);
320
321 return true;
322 }
323 else
324 {
325 this.UpdateMessage = strReceive;
326 string caption = "Verify Code Update";
327 MessageBoxButtons buttons = MessageBoxButtons.OK;
328
329 DialogResult result;
330
331 // Displays the MessageBox.
332 result = MessageBox.Show(strReceive, caption, buttons);
333 return false;
334 }
335 }
336 }
337
338 protected void SendString(TcpClient tcpClient, string cSendString, string cMult)
339 {
340 String encodedString = this.EncodeSendString(cSendString, cMult);
341
342 NetworkStream ns = tcpClient.GetStream();
343 ns.WriteTimeout = 20000;
344 byte[] sendBytes = Encoding.ASCII.GetBytes(encodedString);
345 ns.Write(sendBytes,0,sendBytes.Length);
346 return;
347 }
348
349 protected string EncodeSendString(String cSendString, String cMulti)
350 {
351 String encoded = null;
352
353 int nLen = cSendString.Length;
354 string sLen = nLen.ToString();
355 sLen = sLen.PadLeft(5, '0');
356 encoded = sLen + cSendString;
357
358 nLen += 15;
359 sLen = nLen.ToString();
360 sLen = sLen.PadLeft(5, '0');
361
362 encoded = "{BMX}" + sLen + encoded;
363 encoded = encoded + cMulti;
364
365 return encoded;
366 }
367
368 private string ReceiveString(TcpClient tcpClient)
369 {
370 NetworkStream ns = tcpClient.GetStream();
371 ns.ReadTimeout = 40000;
372
373
374 //TAE: This following is suspect. NetworkSTream Read and ReadTimeout provide
375 //the same behavior. Look at removing in the futuer. For now, this works.
376 int cyclePause = 25;
377 int cycles = 0;
378 DateTime start = DateTime.Now;
379 while (ns.DataAvailable == false)
380 {
381 if (cycles++ > 999)
382 break;
383 if ((DateTime.Now-start).TotalMilliseconds > 40000)
384 break;
385 Thread.Sleep(cyclePause);
386 }
387
388 if (!ns.DataAvailable)
389 {
390 this.Close();
391 throw new Exception("BMXNetBroker.ReceiveString timeout. Connection Closed.");
392 }
393
394 byte[] bReadBuffer = new byte[1024];
395 string sReadBuffer = "";
396 StringBuilder sbAll = new StringBuilder("", 1024);
397 int numberOfBytesRead = 0;
398
399 // Incoming message may be larger than the buffer size.
400
401 bool bFinished = false;
402 bool bStarted = false;
403 int lpBuf = 0;
404 string sError = "";
405 string sAppError = "";
406 do
407 {
408
409 numberOfBytesRead = ns.Read(bReadBuffer, 0, bReadBuffer.Length);
410 if ((numberOfBytesRead == 1)&&(bStarted == false))
411 {
412 //TAE: This following is suspect. If Read is blocking then this sleep is extra.
413 //This is rarely called
414 Thread.Sleep(15);
415 numberOfBytesRead += ns.Read(bReadBuffer,1, bReadBuffer.Length-1);
416 }
417 if (bStarted == false)
418 {
419 //Process error info at beginning of returned string
420 int nErrLen = bReadBuffer[0];
421 int nAppLen = bReadBuffer[bReadBuffer[0]+1];
422 if ((bReadBuffer[2] == 0)&&(bReadBuffer[3] == 0))
423 { //special case: M error trap invoked in SND^XWBTCPC
424 lpBuf += 2;
425 }
426 sError = Encoding.ASCII.GetString(bReadBuffer, lpBuf + 1, nErrLen);
427 if (sError != "")
428 {
429 sAppError = Encoding.ASCII.GetString(bReadBuffer, lpBuf + 1 + nErrLen + 1, nAppLen);
430 throw new BMXNetException(sError);
431 }
432 sAppError = Encoding.ASCII.GetString(bReadBuffer, lpBuf+1+nErrLen+1, nAppLen);
433 lpBuf += (nErrLen + nAppLen + 2);
434 numberOfBytesRead -= (nErrLen + nAppLen + 2);
435 bStarted = true;
436 }
437
438 bFinished = FindChar(bReadBuffer, (char)4) > -1;
439 sReadBuffer = Encoding.ASCII.GetString(bReadBuffer, lpBuf, numberOfBytesRead);
440 lpBuf = 0;
441 if (bFinished)
442 {
443 sbAll.Append(sReadBuffer, 0, numberOfBytesRead -1);
444 }
445 else
446 {
447 sbAll.Append(sReadBuffer);
448 }
449 }
450 while(!bFinished);
451
452 String decodedReceiveString = sbAll.ToString();
453 return decodedReceiveString;
454
455 }
456 public static int FindChar(byte[] c, char y)
457 {
458 int n = 0;
459 int nRet = -1;
460 for (n = 0; n < c.Length; n++)
461 {
462 if (y == (char)c[n])
463 {
464 nRet = n;
465 break;
466 }
467 }
468
469 return nRet;
470 }
471
472 public static int FindChar(string s, char y)
473 {
474 int n = 0;
475 int nRet = -1;
476 foreach (char c in s)
477 {
478 if (y == c)
479 {
480 nRet = n;
481 break;
482 }
483 n++;
484 }
485 return nRet;
486 }
487
488 protected TcpClient OpenConnectionCommon()
489 {
490 try
491 {
492 TcpClient connector = null;
493
494 try
495 {
496 BMXNetSocketBroker broker = (BMXNetSocketBroker)this.Framework.SocketBroker;
497
498 BMXNetSocketConnectionSpec ConnectionSpec = broker.ConnectionSpec;
499 connector = new TcpClient();
500 connector.SendTimeout = ConnectionSpec.SendTimeout;
501 connector.ReceiveTimeout = ConnectionSpec.ReceiveTimeout;
502 connector.Connect(ConnectionSpec.Server, ConnectionSpec.Port);
503 }
504 catch (SocketException exSocket)
505 {
506 string s = exSocket.Message + exSocket.StackTrace;
507 throw new BMXNetException(s);
508 }
509
510 //Prepare & send the connect message
511 string cSend = "TCPconnect^" + "" + "^^";
512 int nLen = cSend.Length;
513 string sLen = nLen.ToString();
514 sLen = sLen.PadLeft(5, '0');
515 cSend = "{BMX}" + sLen + cSend;
516
517 NetworkStream ns = connector.GetStream();
518 byte[] sendBytes = Encoding.ASCII.GetBytes(cSend);
519 ns.Write(sendBytes,0,sendBytes.Length);
520
521 return connector;
522 }
523 catch (BMXNetException bmxEx)
524 {
525 throw bmxEx;
526 }
527 catch (Exception ex)
528 {
529 string s = ex.Message + ex.StackTrace;
530 throw new BMXNetException(s);
531 }
532 }//End OpenConnectionCommon
533
534 }
535 /// <summary>
536 /// Class for passing result of AttemptChangeVerifyCode to calling routine
537 /// </summary>
538 public class VerifyCodeUpdateResult
539 {
540 //Members
541 private DialogResult dr = new DialogResult();
542 private bool bVerifyCodeWasSucessfullyChanged;
543
544 //Properties
545 public DialogResult DialogResult
546 {
547 set { this.dr = value; }
548 get { return this.dr; }
549 }
550
551 public bool WasVerifyCodeUpdatedSuccessfully
552 {
553 set { this.bVerifyCodeWasSucessfullyChanged = value; }
554 get { return this.bVerifyCodeWasSucessfullyChanged; }
555 }
556
557
558 }
559}
Note: See TracBrowser for help on using the repository browser.