| | | 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; |
| | | 5 | | using System.Security.Principal; |
| | | 6 | | using System.Xml; |
| | | 7 | | using CoreWCF.IdentityModel.Claims; |
| | | 8 | | using CoreWCF.Runtime; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF |
| | | 11 | | { |
| | | 12 | | public class UpnEndpointIdentity : EndpointIdentity |
| | | 13 | | { |
| | | 14 | | private SecurityIdentifier _upnSid; |
| | | 15 | | private bool _hasUpnSidBeenComputed; |
| | 0 | 16 | | private readonly object _thisLock = new object(); |
| | | 17 | | |
| | 0 | 18 | | public UpnEndpointIdentity(string upnName) |
| | | 19 | | { |
| | 0 | 20 | | if (upnName == null) |
| | | 21 | | { |
| | 0 | 22 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(upnName)); |
| | | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | Initialize(Claim.CreateUpnClaim(upnName)); |
| | 0 | 26 | | _hasUpnSidBeenComputed = false; |
| | 0 | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | public UpnEndpointIdentity(Claim identity) |
| | | 30 | | { |
| | 0 | 31 | | if (identity == null) |
| | | 32 | | { |
| | 0 | 33 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identity)); |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | if (!identity.ClaimType.Equals(ClaimTypes.Upn)) |
| | | 37 | | { |
| | 0 | 38 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.UnrecognizedClaimTypeForIdenti |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | Initialize(identity); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | internal override void WriteContentsTo(XmlDictionaryWriter writer) |
| | | 45 | | { |
| | 0 | 46 | | if (writer == null) |
| | | 47 | | { |
| | 0 | 48 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | writer.WriteElementString(XD.AddressingDictionary.Upn, XD.AddressingDictionary.IdentityExtensionNamespace, ( |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | internal SecurityIdentifier GetUpnSid() |
| | | 55 | | { |
| | | 56 | | Fx.Assert(ClaimTypes.Upn.Equals(IdentityClaim.ClaimType), ""); |
| | 0 | 57 | | if (!_hasUpnSidBeenComputed) |
| | | 58 | | { |
| | 0 | 59 | | lock (_thisLock) |
| | | 60 | | { |
| | 0 | 61 | | string upn = (string)IdentityClaim.Resource; |
| | 0 | 62 | | if (!_hasUpnSidBeenComputed) |
| | | 63 | | { |
| | | 64 | | try |
| | | 65 | | { |
| | 0 | 66 | | var userAccount = new NTAccount(upn); |
| | 0 | 67 | | _upnSid = userAccount.Translate(typeof(SecurityIdentifier)) as SecurityIdentifier; |
| | 0 | 68 | | } |
| | | 69 | | catch (Exception e) |
| | | 70 | | { |
| | | 71 | | // Always immediately rethrow fatal exceptions. |
| | 0 | 72 | | if (Fx.IsFatal(e)) |
| | | 73 | | { |
| | 0 | 74 | | throw; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | if (e is NullReferenceException) |
| | | 78 | | { |
| | 0 | 79 | | throw; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | //SecurityTraceRecordHelper.TraceSpnToSidMappingFailure(upn, e); |
| | 0 | 83 | | } |
| | | 84 | | finally |
| | | 85 | | { |
| | 0 | 86 | | _hasUpnSidBeenComputed = true; |
| | 0 | 87 | | } |
| | | 88 | | } |
| | 0 | 89 | | } |
| | | 90 | | } |
| | 0 | 91 | | return _upnSid; |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |