PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSFTPDownload/UICredentialsProvider.cs

#
C# | 101 lines | 69 code | 15 blank | 17 comment | 7 complexity | dffc85e2777cec388c9b2a819701297e MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: UICredentialsProvider.cs
  3. * Project: CSFTPDownload
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * The Form UICredentialsProvider contains 3 textboxes that accept UserName,
  7. * Password and Domain to initialize a NetworkCredential instance.
  8. *
  9. * This source is subject to the Microsoft Public License.
  10. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  11. * All other rights reserved.
  12. *
  13. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  14. * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  16. \***************************************************************************/
  17. using System;
  18. using System.Net;
  19. using System.Windows.Forms;
  20. namespace CSFTPDownload
  21. {
  22. public partial class UICredentialsProvider : Form
  23. {
  24. public NetworkCredential Credentials { get; set; }
  25. bool useOriginalCredentials = true;
  26. public UICredentialsProvider()
  27. : this(null)
  28. { }
  29. public UICredentialsProvider(NetworkCredential credentials)
  30. {
  31. InitializeComponent();
  32. this.Credentials = credentials;
  33. if (this.Credentials != null)
  34. {
  35. this.tbUserName.Text = this.Credentials.UserName;
  36. this.tbDomain.Text = this.Credentials.Domain;
  37. this.tbPassword.Text = this.Credentials.Password;
  38. useOriginalCredentials = true;
  39. }
  40. else
  41. {
  42. useOriginalCredentials = false;
  43. }
  44. }
  45. private void btnOK_Click(object sender, EventArgs e)
  46. {
  47. if (!useOriginalCredentials)
  48. {
  49. if (chkAnonymous.Checked)
  50. {
  51. // Use Anonymous Credentials by default.
  52. Credentials = new NetworkCredential("Anonymous", "");
  53. }
  54. else if (String.IsNullOrEmpty(tbUserName.Text)
  55. || String.IsNullOrEmpty(tbPassword.Text))
  56. {
  57. MessageBox.Show("Please type the user name and password!");
  58. return;
  59. }
  60. else
  61. {
  62. Credentials = new NetworkCredential(
  63. tbUserName.Text.Trim(),
  64. tbPassword.Text,
  65. tbDomain.Text.Trim());
  66. }
  67. }
  68. this.DialogResult = System.Windows.Forms.DialogResult.OK;
  69. }
  70. private void btnCancel_Click(object sender, EventArgs e)
  71. {
  72. this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  73. }
  74. private void chkAnonymous_CheckedChanged(object sender, EventArgs e)
  75. {
  76. tbDomain.Enabled = !chkAnonymous.Checked;
  77. tbPassword.Enabled = !chkAnonymous.Checked;
  78. tbUserName.Enabled = !chkAnonymous.Checked;
  79. useOriginalCredentials = false;
  80. }
  81. private void tb_TextChanged(object sender, EventArgs e)
  82. {
  83. useOriginalCredentials = false;
  84. }
  85. }
  86. }