PageRenderTime 17ms CodeModel.GetById 8ms app.highlight 7ms RepoModel.GetById 1ms 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
 18using System;
 19using System.Net;
 20using System.Windows.Forms;
 21
 22namespace CSFTPDownload
 23{
 24    public partial class UICredentialsProvider : Form
 25    {
 26        public NetworkCredential Credentials { get; set; }
 27
 28        bool useOriginalCredentials = true;
 29
 30        public UICredentialsProvider()
 31            : this(null)
 32        { }
 33
 34        public UICredentialsProvider(NetworkCredential credentials)
 35        {
 36
 37            InitializeComponent();
 38
 39            this.Credentials = credentials;
 40
 41            if (this.Credentials != null)
 42            {
 43                this.tbUserName.Text = this.Credentials.UserName;
 44                this.tbDomain.Text = this.Credentials.Domain;
 45                this.tbPassword.Text = this.Credentials.Password;
 46                useOriginalCredentials = true;
 47            }
 48            else
 49            {
 50                useOriginalCredentials = false;
 51            }
 52        }
 53
 54        private void btnOK_Click(object sender, EventArgs e)
 55        {
 56            if (!useOriginalCredentials)
 57            {
 58                if (chkAnonymous.Checked)
 59                {
 60
 61                    // Use Anonymous Credentials by default.
 62                    Credentials = new NetworkCredential("Anonymous", "");
 63                }
 64                else if (String.IsNullOrEmpty(tbUserName.Text)
 65                        || String.IsNullOrEmpty(tbPassword.Text))
 66                {
 67                    MessageBox.Show("Please type the user name and password!");
 68                    return;
 69                }
 70                else
 71                {
 72                    Credentials = new NetworkCredential(
 73                        tbUserName.Text.Trim(),
 74                        tbPassword.Text,
 75                        tbDomain.Text.Trim());
 76                }
 77            }
 78            this.DialogResult = System.Windows.Forms.DialogResult.OK;
 79
 80        }
 81
 82        private void btnCancel_Click(object sender, EventArgs e)
 83        {
 84            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
 85        }
 86
 87        private void chkAnonymous_CheckedChanged(object sender, EventArgs e)
 88        {
 89            tbDomain.Enabled = !chkAnonymous.Checked;
 90            tbPassword.Enabled = !chkAnonymous.Checked;
 91            tbUserName.Enabled = !chkAnonymous.Checked;
 92
 93            useOriginalCredentials = false;
 94        }
 95
 96        private void tb_TextChanged(object sender, EventArgs e)
 97        {
 98            useOriginalCredentials = false;
 99        }
100    }
101}