| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Runtime.InteropServices; |
| | | 6 | | using System.Security; |
| | | 7 | | |
| | | 8 | | namespace System.Net |
| | | 9 | | { |
| | | 10 | | /// <devdoc> |
| | | 11 | | /// <para> |
| | | 12 | | /// Provides credentials for password-based authentication schemes such as basic, |
| | | 13 | | /// digest, NTLM and Kerberos. |
| | | 14 | | /// </para> |
| | | 15 | | /// </devdoc> |
| | | 16 | | public class NetworkCredential : ICredentials, ICredentialsByHost |
| | | 17 | | { |
| | | 18 | | private string _domain; |
| | 0 | 19 | | private string _userName = string.Empty; |
| | | 20 | | private object? _password; |
| | | 21 | | |
| | | 22 | | public NetworkCredential() |
| | 0 | 23 | | : this(string.Empty, string.Empty, string.Empty) |
| | 0 | 24 | | { |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <devdoc> |
| | | 28 | | /// <para> |
| | | 29 | | /// Initializes a new instance of the <see cref='System.Net.NetworkCredential'/> |
| | | 30 | | /// class with name and password set as specified. |
| | | 31 | | /// </para> |
| | | 32 | | /// </devdoc> |
| | | 33 | | public NetworkCredential(string? userName, string? password) |
| | 0 | 34 | | : this(userName, password, string.Empty) |
| | 0 | 35 | | { |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <devdoc> |
| | | 39 | | /// <para> |
| | | 40 | | /// Initializes a new instance of the <see cref='System.Net.NetworkCredential'/> |
| | | 41 | | /// class with name, password and domain set as specified. |
| | | 42 | | /// </para> |
| | | 43 | | /// </devdoc> |
| | 0 | 44 | | public NetworkCredential(string? userName, string? password, string? domain) |
| | 0 | 45 | | { |
| | 0 | 46 | | UserName = userName; |
| | 0 | 47 | | Password = password; |
| | 0 | 48 | | Domain = domain; |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | [CLSCompliant(false)] |
| | | 52 | | public NetworkCredential(string? userName, SecureString? password) |
| | 0 | 53 | | : this(userName, password, string.Empty) |
| | 0 | 54 | | { |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | [CLSCompliant(false)] |
| | 0 | 58 | | public NetworkCredential(string? userName, SecureString? password, string? domain) |
| | 0 | 59 | | { |
| | 0 | 60 | | UserName = userName; |
| | 0 | 61 | | SecurePassword = password; |
| | 0 | 62 | | Domain = domain; |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <devdoc> |
| | | 66 | | /// <para> |
| | | 67 | | /// The user name associated with this credential. |
| | | 68 | | /// </para> |
| | | 69 | | /// </devdoc> |
| | | 70 | | [AllowNull] |
| | | 71 | | public string UserName |
| | | 72 | | { |
| | 0 | 73 | | get { return _userName; } |
| | 0 | 74 | | set { _userName = value ?? string.Empty; } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <devdoc> |
| | | 78 | | /// <para> |
| | | 79 | | /// The password for the user name. |
| | | 80 | | /// </para> |
| | | 81 | | /// </devdoc> |
| | | 82 | | [AllowNull] |
| | | 83 | | public string Password |
| | | 84 | | { |
| | | 85 | | get |
| | 0 | 86 | | { |
| | 0 | 87 | | SecureString? sstr = _password as SecureString; |
| | 0 | 88 | | if (sstr != null) |
| | 0 | 89 | | { |
| | 0 | 90 | | return MarshalToString(sstr); |
| | | 91 | | } |
| | 0 | 92 | | return (string?)_password ?? string.Empty; |
| | 0 | 93 | | } |
| | | 94 | | set |
| | 0 | 95 | | { |
| | 0 | 96 | | SecureString? old = _password as SecureString; |
| | 0 | 97 | | _password = value; |
| | 0 | 98 | | old?.Dispose(); |
| | 0 | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | [CLSCompliant(false)] |
| | | 103 | | [AllowNull] |
| | | 104 | | public SecureString SecurePassword |
| | | 105 | | { |
| | | 106 | | get |
| | 0 | 107 | | { |
| | 0 | 108 | | string? str = _password as string; |
| | 0 | 109 | | if (str != null) |
| | 0 | 110 | | { |
| | 0 | 111 | | return MarshalToSecureString(str); |
| | | 112 | | } |
| | 0 | 113 | | SecureString? sstr = _password as SecureString; |
| | 0 | 114 | | return sstr != null ? sstr.Copy() : new SecureString(); |
| | 0 | 115 | | } |
| | | 116 | | set |
| | 0 | 117 | | { |
| | 0 | 118 | | SecureString? old = _password as SecureString; |
| | 0 | 119 | | _password = value?.Copy(); |
| | 0 | 120 | | old?.Dispose(); |
| | 0 | 121 | | } |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <devdoc> |
| | | 125 | | /// <para> |
| | | 126 | | /// The machine name that verifies |
| | | 127 | | /// the credentials. Usually this is the host machine. |
| | | 128 | | /// </para> |
| | | 129 | | /// </devdoc> |
| | | 130 | | [AllowNull] |
| | | 131 | | public string Domain |
| | | 132 | | { |
| | 0 | 133 | | get { return _domain; } |
| | | 134 | | [MemberNotNull(nameof(_domain))] |
| | 0 | 135 | | set { _domain = value ?? string.Empty; } |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <devdoc> |
| | | 139 | | /// <para> |
| | | 140 | | /// Returns an instance of the NetworkCredential class for a Uri and |
| | | 141 | | /// authentication type. |
| | | 142 | | /// </para> |
| | | 143 | | /// </devdoc> |
| | | 144 | | public NetworkCredential GetCredential(Uri? uri, string? authenticationType) |
| | 0 | 145 | | { |
| | 0 | 146 | | return this; |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | public NetworkCredential GetCredential(string? host, int port, string? authenticationType) |
| | 0 | 150 | | { |
| | 0 | 151 | | return this; |
| | 0 | 152 | | } |
| | | 153 | | |
| | | 154 | | private static string MarshalToString(SecureString sstr) |
| | 0 | 155 | | { |
| | 0 | 156 | | if (sstr == null || sstr.Length == 0) |
| | 0 | 157 | | { |
| | 0 | 158 | | return string.Empty; |
| | | 159 | | } |
| | | 160 | | |
| | 0 | 161 | | IntPtr ptr = IntPtr.Zero; |
| | 0 | 162 | | string result = string.Empty; |
| | | 163 | | try |
| | 0 | 164 | | { |
| | 0 | 165 | | ptr = Marshal.SecureStringToGlobalAllocUnicode(sstr); |
| | 0 | 166 | | result = Marshal.PtrToStringUni(ptr)!; |
| | 0 | 167 | | } |
| | | 168 | | finally |
| | 0 | 169 | | { |
| | 0 | 170 | | if (ptr != IntPtr.Zero) |
| | 0 | 171 | | { |
| | 0 | 172 | | Marshal.ZeroFreeGlobalAllocUnicode(ptr); |
| | 0 | 173 | | } |
| | 0 | 174 | | } |
| | 0 | 175 | | return result; |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | private unsafe SecureString MarshalToSecureString(string str) |
| | 0 | 179 | | { |
| | 0 | 180 | | if (string.IsNullOrEmpty(str)) |
| | 0 | 181 | | { |
| | 0 | 182 | | return new SecureString(); |
| | | 183 | | } |
| | | 184 | | |
| | 0 | 185 | | fixed (char* ptr = str) |
| | 0 | 186 | | { |
| | 0 | 187 | | return new SecureString(ptr, str.Length); |
| | | 188 | | } |
| | 0 | 189 | | } |
| | | 190 | | } |
| | | 191 | | } |
| | | 192 | | |