1 //------------------------------------------------------------------------------
2 // <copyright file="NetworkCredential.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
11 namespace System.Net {
14 using System.Runtime.InteropServices;
15 using System.Security;
16 using System.Security.Cryptography;
17 using System.Security.Permissions;
19 using System.Threading;
20 using Microsoft.Win32;
24 /// <para>Provides credentials for password-based
25 /// authentication schemes such as basic, digest, NTLM and Kerberos.</para>
27 public class NetworkCredential : ICredentials,ICredentialsByHost {
30 private static volatile EnvironmentPermission m_environmentUserNamePermission;
31 private static volatile EnvironmentPermission m_environmentDomainNamePermission;
32 private static readonly object lockingObject = new object();
34 private string m_domain;
35 private string m_userName;
37 private SecureString m_password;
39 private string m_password;
42 public NetworkCredential()
43 : this(string.Empty, string.Empty, string.Empty) {
48 /// Initializes a new instance of the <see cref='System.Net.NetworkCredential'/>
49 /// class with name and password set as specified.
52 public NetworkCredential(string userName, string password)
53 : this(userName, password, string.Empty) {
59 /// Initializes a new instance of the <see cref='System.Net.NetworkCredential'/>
60 /// class with name and password set as specified.
63 public NetworkCredential(string userName, SecureString password)
64 : this(userName, password, string.Empty) {
70 /// Initializes a new instance of the <see cref='System.Net.NetworkCredential'/>
71 /// class with name and password set as specified.
74 public NetworkCredential(string userName, string password, string domain) {
83 /// Initializes a new instance of the <see cref='System.Net.NetworkCredential'/>
84 /// class with name and password set as specified.
87 public NetworkCredential(string userName, SecureString password, string domain) {
89 SecurePassword = password;
95 void InitializePart1() {
96 if (m_environmentUserNamePermission == null) {
98 if (m_environmentUserNamePermission == null) {
99 m_environmentDomainNamePermission = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERDOMAIN");
100 m_environmentUserNamePermission = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME");
109 /// The user name associated with this credential.
112 public string UserName {
116 m_environmentUserNamePermission.Demand();
118 return InternalGetUserName();
122 m_userName = String.Empty;
125 // GlobalLog.Print("NetworkCredential::set_UserName: m_userName: \"" + m_userName + "\"" );
131 /// The password for the user name.
134 public string Password {
137 ExceptionHelper.UnmanagedPermission.Demand();
139 return InternalGetPassword();
144 m_password = String.Empty;
147 // GlobalLog.Print("NetworkCredential::set_Password: m_password: \"" + m_password + "\"" );
149 m_password = UnsafeNclNativeMethods.SecureStringHelper.CreateSecureString(value);
150 // GlobalLog.Print("NetworkCredential::set_Password: value = " + value);
151 // GlobalLog.Print("NetworkCredential::set_Password: m_password:");
152 // GlobalLog.Dump(m_password);
153 #endif //!FEATURE_PAL
160 /// The password for the user name.
163 public SecureString SecurePassword {
166 ExceptionHelper.UnmanagedPermission.Demand();
168 return InternalGetSecurePassword().Copy();
172 m_password = new SecureString(); // makes 0 length string
174 m_password = value.Copy();
177 #endif //!FEATURE_PAL
181 /// The machine name that verifies
182 /// the credentials. Usually this is the host machine.
185 public string Domain {
189 m_environmentDomainNamePermission.Demand();
191 return InternalGetDomain();
195 m_domain = String.Empty;
198 // GlobalLog.Print("NetworkCredential::set_Domain: m_domain: \"" + m_domain + "\"" );
202 internal string InternalGetUserName() {
203 // GlobalLog.Print("NetworkCredential::get_UserName: returning \"" + m_userName + "\"");
207 internal string InternalGetPassword() {
209 // GlobalLog.Print("NetworkCredential::get_Password: returning \"" + m_password + "\"");
212 string decryptedString = UnsafeNclNativeMethods.SecureStringHelper.CreateString(m_password);
214 // GlobalLog.Print("NetworkCredential::get_Password: returning \"" + decryptedString + "\"");
215 return decryptedString;
216 #endif //!FEATURE_PAL
220 internal SecureString InternalGetSecurePassword()
224 #endif //!FEATURE_PAL
226 internal string InternalGetDomain()
228 // GlobalLog.Print("NetworkCredential::get_Domain: returning \"" + m_domain + "\"");
232 internal string InternalGetDomainUserName() {
233 string domainUserName = InternalGetDomain();
234 if (domainUserName.Length != 0)
235 domainUserName += "\\";
236 domainUserName += InternalGetUserName();
237 return domainUserName;
242 /// Returns an instance of the NetworkCredential class for a Uri and
243 /// authentication type.
246 public NetworkCredential GetCredential(Uri uri, String authType) {
250 public NetworkCredential GetCredential(string host, int port, String authenticationType) {
255 // this method is only called as part of an assert
256 internal bool IsEqualTo(object compObject) {
257 if ((object)compObject == null)
259 if ((object)this == (object)compObject)
261 NetworkCredential compCred = compObject as NetworkCredential;
262 if ((object)compCred == null)
265 return(InternalGetUserName() == compCred.InternalGetUserName() &&
266 InternalGetPassword() == compCred.InternalGetPassword() &&
267 InternalGetDomain() == compCred.InternalGetDomain());
269 return (InternalGetUserName() == compCred.InternalGetUserName() &&
270 InternalGetDomain() == compCred.InternalGetDomain() &&
271 UnsafeNclNativeMethods.SecureStringHelper.AreEqualValues(InternalGetSecurePassword(),
272 compCred.InternalGetSecurePassword()));
273 #endif //!FEATURE_PAL
276 } // class NetworkCredential
277 } // namespace System.Net