< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 83
Coverable lines: 83
Total lines: 192
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 28
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
.ctor()100%110%
.ctor(...)100%110%
.ctor(...)100%110%
.ctor(...)100%110%
GetCredential(...)100%110%
GetCredential(...)100%110%
MarshalToString(...)0%660%
MarshalToSecureString(...)0%440%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Net.Primitives/src/System/Net/NetworkCredential.cs

#LineLine coverage
 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
 4using System.Diagnostics.CodeAnalysis;
 5using System.Runtime.InteropServices;
 6using System.Security;
 7
 8namespace 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;
 019        private string _userName = string.Empty;
 20        private object? _password;
 21
 22        public NetworkCredential()
 023            : this(string.Empty, string.Empty, string.Empty)
 024        {
 025        }
 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)
 034        : this(userName, password, string.Empty)
 035        {
 036        }
 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>
 044        public NetworkCredential(string? userName, string? password, string? domain)
 045        {
 046            UserName = userName;
 047            Password = password;
 048            Domain = domain;
 049        }
 50
 51        [CLSCompliant(false)]
 52        public NetworkCredential(string? userName, SecureString? password)
 053        : this(userName, password, string.Empty)
 054        {
 055        }
 56
 57        [CLSCompliant(false)]
 058        public NetworkCredential(string? userName, SecureString? password, string? domain)
 059        {
 060            UserName = userName;
 061            SecurePassword = password;
 062            Domain = domain;
 063        }
 64
 65        /// <devdoc>
 66        ///    <para>
 67        ///       The user name associated with this credential.
 68        ///    </para>
 69        /// </devdoc>
 70        [AllowNull]
 71        public string UserName
 72        {
 073            get { return _userName; }
 074            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
 086            {
 087                SecureString? sstr = _password as SecureString;
 088                if (sstr != null)
 089                {
 090                    return MarshalToString(sstr);
 91                }
 092                return (string?)_password ?? string.Empty;
 093            }
 94            set
 095            {
 096                SecureString? old = _password as SecureString;
 097                _password = value;
 098                old?.Dispose();
 099            }
 100        }
 101
 102        [CLSCompliant(false)]
 103        [AllowNull]
 104        public SecureString SecurePassword
 105        {
 106            get
 0107            {
 0108                string? str = _password as string;
 0109                if (str != null)
 0110                {
 0111                    return MarshalToSecureString(str);
 112                }
 0113                SecureString? sstr = _password as SecureString;
 0114                return sstr != null ? sstr.Copy() : new SecureString();
 0115            }
 116            set
 0117            {
 0118                SecureString? old = _password as SecureString;
 0119                _password = value?.Copy();
 0120                old?.Dispose();
 0121            }
 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        {
 0133            get { return _domain; }
 134            [MemberNotNull(nameof(_domain))]
 0135            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)
 0145        {
 0146            return this;
 0147        }
 148
 149        public NetworkCredential GetCredential(string? host, int port, string? authenticationType)
 0150        {
 0151            return this;
 0152        }
 153
 154        private static string MarshalToString(SecureString sstr)
 0155        {
 0156            if (sstr == null || sstr.Length == 0)
 0157            {
 0158                return string.Empty;
 159            }
 160
 0161            IntPtr ptr = IntPtr.Zero;
 0162            string result = string.Empty;
 163            try
 0164            {
 0165                ptr = Marshal.SecureStringToGlobalAllocUnicode(sstr);
 0166                result = Marshal.PtrToStringUni(ptr)!;
 0167            }
 168            finally
 0169            {
 0170                if (ptr != IntPtr.Zero)
 0171                {
 0172                    Marshal.ZeroFreeGlobalAllocUnicode(ptr);
 0173                }
 0174            }
 0175            return result;
 0176        }
 177
 178        private unsafe SecureString MarshalToSecureString(string str)
 0179        {
 0180            if (string.IsNullOrEmpty(str))
 0181            {
 0182                return new SecureString();
 183            }
 184
 0185            fixed (char* ptr = str)
 0186            {
 0187                return new SecureString(ptr, str.Length);
 188            }
 0189        }
 190    }
 191}
 192