| | | 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.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Globalization; |
| | | 9 | | |
| | | 10 | | namespace System.Net.Http |
| | | 11 | | { |
| | | 12 | | internal sealed class PreAuthCredentialCache |
| | | 13 | | { |
| | | 14 | | private Dictionary<CredentialCacheKey, NetworkCredential>? _cache; |
| | | 15 | | |
| | | 16 | | public void Add(Uri uriPrefix, string authType, NetworkCredential cred) |
| | 0 | 17 | | { |
| | 0 | 18 | | Debug.Assert(uriPrefix != null); |
| | 0 | 19 | | Debug.Assert(authType != null); |
| | | 20 | | |
| | 0 | 21 | | var key = new CredentialCacheKey(uriPrefix, authType); |
| | | 22 | | |
| | 0 | 23 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"Adding key:[{key}], cred:[{cred.Domain}],[{c |
| | | 24 | | |
| | 0 | 25 | | _cache ??= new Dictionary<CredentialCacheKey, NetworkCredential>(); |
| | 0 | 26 | | _cache.Add(key, cred); |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public void Remove(Uri uriPrefix, string authType) |
| | 0 | 30 | | { |
| | 0 | 31 | | Debug.Assert(uriPrefix != null); |
| | 0 | 32 | | Debug.Assert(authType != null); |
| | | 33 | | |
| | 0 | 34 | | if (_cache == null) |
| | 0 | 35 | | { |
| | 0 | 36 | | return; |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | var key = new CredentialCacheKey(uriPrefix, authType); |
| | 0 | 40 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"Removing key:[{key}]"); |
| | 0 | 41 | | _cache.Remove(key); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | public (Uri uriPrefix, NetworkCredential credential)? GetCredential(Uri uriPrefix, string authType) |
| | 0 | 45 | | { |
| | 0 | 46 | | Debug.Assert(uriPrefix != null); |
| | 0 | 47 | | Debug.Assert(authType != null); |
| | | 48 | | |
| | 0 | 49 | | if (_cache == null) |
| | 0 | 50 | | { |
| | 0 | 51 | | return null; |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | CredentialCacheHelper.TryGetCredential(_cache, uriPrefix, authType, out Uri? mostSpecificMatchUri, out Netwo |
| | | 55 | | |
| | 0 | 56 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"Returning {(mostSpecificMatch == null ? "nul |
| | | 57 | | |
| | 0 | 58 | | return mostSpecificMatch == null ? null : (mostSpecificMatchUri!, mostSpecificMatch!); |
| | 0 | 59 | | } |
| | | 60 | | } |
| | | 61 | | } |