The Labs \ Source Viewer \ SSCLI \ System.Net \ NetworkCredential

  1. //------------------------------------------------------------------------------
  2. // <copyright file="NetworkCredential.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Net
  16. {
  17.    
  18.     using System.IO;
  19.     using System.Runtime.InteropServices;
  20.     using System.Security.Cryptography;
  21.     using System.Security.Permissions;
  22.     using System.Text;
  23.     using System.Threading;
  24.     using Microsoft.Win32;
  25.    
  26.    
  27.     /// <devdoc>
  28.     /// <para>Provides credentials for password-based
  29.     /// authentication schemes such as basic, digest, NTLM and Kerberos.</para>
  30.     /// </devdoc>
  31.     public class NetworkCredential : ICredentials, ICredentialsByHost
  32.     {
  33.        
  34.         private static EnvironmentPermission m_environmentUserNamePermission;
  35.         private static EnvironmentPermission m_environmentDomainNamePermission;
  36.         private static readonly object lockingObject = new object();
  37.         private string m_userName;
  38.         private string m_password;
  39.         private string m_domain;
  40.        
  41.        
  42.         public NetworkCredential()
  43.         {
  44.         }
  45.        
  46.         /// <devdoc>
  47.         /// <para>
  48.         /// Initializes a new instance of the <see cref='System.Net.NetworkCredential'/>
  49.         /// class with name and password set as specified.
  50.         /// </para>
  51.         /// </devdoc>
  52.         public NetworkCredential(string userName, string password) : this(userName, password, string.Empty)
  53.         {
  54.         }
  55.        
  56.         /// <devdoc>
  57.         /// <para>
  58.         /// Initializes a new instance of the <see cref='System.Net.NetworkCredential'/>
  59.         /// class with name and password set as specified.
  60.         /// </para>
  61.         /// </devdoc>
  62.         public NetworkCredential(string userName, string password, string domain) : this(userName, password, domain, true)
  63.         {
  64.         }
  65.        
  66.         /// <devdoc>
  67.         /// <para>
  68.         /// Initializes a new instance of the <see cref='System.Net.NetworkCredential'/>
  69.         /// class with name and password set as specified.
  70.         /// </para>
  71.         /// </devdoc>
  72.        
  73.        
  74.         internal NetworkCredential(string userName, string password, string domain, bool encrypt)
  75.         {
  76.             UserName = userName;
  77.             Password = password;
  78.             Domain = domain;
  79.         }
  80.        
  81.        
  82.         void InitializePart1()
  83.         {
  84.             if (m_environmentUserNamePermission == null) {
  85.                 lock (lockingObject) {
  86.                     if (m_environmentUserNamePermission == null) {
  87.                         m_environmentDomainNamePermission = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERDOMAIN");
  88.                         m_environmentUserNamePermission = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME");
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.        
  94.        
  95.         void InitializePart2()
  96.         {
  97.            
  98.         }
  99.        
  100.         /// <devdoc>
  101.         /// <para>
  102.         /// The user name associated with this credential.
  103.         /// </para>
  104.         /// </devdoc>
  105.         public string UserName {
  106.             get {
  107.                 InitializePart1();
  108.                 m_environmentUserNamePermission.Demand();
  109.                 return InternalGetUserName();
  110.             }
  111.                 // GlobalLog.Print("NetworkCredential::set_UserName: m_userName: \"" + m_userName + "\"" );
  112.             set { m_userName = value; }
  113.         }
  114.        
  115.         /// <devdoc>
  116.         /// <para>
  117.         /// The password for the user name.
  118.         /// </para>
  119.         /// </devdoc>
  120.         public string Password {
  121.             get {
  122.                 ExceptionHelper.UnmanagedPermission.Demand();
  123.                 return InternalGetPassword();
  124.             }
  125.                 // GlobalLog.Print("NetworkCredential::set_Password: m_password: \"" + m_password + "\"" );
  126.             set { m_password = value; }
  127.         }
  128.        
  129.         /// <devdoc>
  130.         /// <para>
  131.         /// The machine name that verifies
  132.         /// the credentials. Usually this is the host machine.
  133.         /// </para>
  134.         /// </devdoc>
  135.         public string Domain {
  136.             get {
  137.                 InitializePart1();
  138.                 m_environmentDomainNamePermission.Demand();
  139.                 return InternalGetDomain();
  140.             }
  141.                 // GlobalLog.Print("NetworkCredential::set_Domain: m_domain: \"" + m_domain + "\"" );
  142.             set { m_domain = value; }
  143.         }
  144.        
  145.         internal string InternalGetUserName()
  146.         {
  147.             // GlobalLog.Print("NetworkCredential::get_UserName: returning \"" + m_userName + "\"");
  148.             return m_userName;
  149.         }
  150.        
  151.         internal string InternalGetPassword()
  152.         {
  153.             // GlobalLog.Print("NetworkCredential::get_Password: returning \"" + m_password + "\"");
  154.             return m_password;
  155.         }
  156.        
  157.         internal string InternalGetDomain()
  158.         {
  159.            
  160.             // GlobalLog.Print("NetworkCredential::get_Domain: returning \"" + m_domain + "\"");
  161.             return m_domain;
  162.         }
  163.        
  164.         internal string InternalGetDomainUserName()
  165.         {
  166.             string domainUserName = InternalGetDomain();
  167.             if (domainUserName.Length != 0)
  168.                 domainUserName += "\\";
  169.             domainUserName += InternalGetUserName();
  170.             return domainUserName;
  171.         }
  172.        
  173.         /// <devdoc>
  174.         /// <para>
  175.         /// Returns an instance of the NetworkCredential class for a Uri and
  176.         /// authentication type.
  177.         /// </para>
  178.         /// </devdoc>
  179.         public NetworkCredential GetCredential(Uri uri, string authType)
  180.         {
  181.             return this;
  182.         }
  183.        
  184.         public NetworkCredential GetCredential(string host, int port, string authenticationType)
  185.         {
  186.             return this;
  187.         }
  188.        
  189.         internal bool IsEqualTo(object compObject)
  190.         {
  191.             if ((object)compObject == null)
  192.                 return false;
  193.             if ((object)this == (object)compObject)
  194.                 return true;
  195.             NetworkCredential compCred = compObject as NetworkCredential;
  196.             if ((object)compCred == null)
  197.                 return false;
  198.             return (InternalGetUserName() == compCred.InternalGetUserName() && InternalGetPassword() == compCred.InternalGetPassword() && InternalGetDomain() == compCred.InternalGetDomain());
  199.         }
  200.        
  201.        
  202.     }
  203.     // class NetworkCredential
  204. }
  205. // namespace System.Net

Developer Fusion