Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WindowsIdentity.cs
Go to the documentation of this file.
8using System.Text;
12
14
16{
18
20
22
24
25 private string _name;
26
28
30
32
34
35 private readonly string _authType;
36
37 private int _isAuthenticated = -1;
38
40
41 private volatile bool _impersonationLevelInitialized;
42
43 public new const string DefaultIssuer = "AD AUTHORITY";
44
45 private readonly string _issuerName = "AD AUTHORITY";
46
47 private object _claimsIntiailizedLock;
48
49 private bool _claimsInitialized;
50
52
54
55 private static bool s_ignoreWindows8Properties;
56
58
59 public sealed override string? AuthenticationType
60 {
61 get
62 {
64 {
65 return string.Empty;
66 }
67 if (_authType == null)
68 {
69 global::Interop.LUID LogonId = GetLogonAuthId(_safeTokenHandle);
70 if (LogonId.LowPart == 998)
71 {
72 return string.Empty;
73 }
75 try
76 {
77 int num = global::Interop.SspiCli.LsaGetLogonSessionData(ref LogonId, out ppLogonSessionData);
78 if (num < 0)
79 {
80 throw GetExceptionFromNtStatus(num);
81 }
82 ppLogonSessionData.Initialize((uint)Marshal.SizeOf<global::Interop.SECURITY_LOGON_SESSION_DATA>());
83 return Marshal.PtrToStringUni(ppLogonSessionData.Read<global::Interop.SECURITY_LOGON_SESSION_DATA>(0uL).AuthenticationPackage.Buffer);
84 }
85 finally
86 {
87 ppLogonSessionData?.Dispose();
88 }
89 }
90 return _authType;
91 }
92 }
93
124
125 public override bool IsAuthenticated
126 {
127 get
128 {
129 if (_isAuthenticated == -1)
130 {
131 if ((object)s_authenticatedUserRid == null)
132 {
133 s_authenticatedUserRid = new SecurityIdentifier(IdentifierAuthority.NTAuthority, new int[1] { 11 });
134 }
136 }
137 return _isAuthenticated == 1;
138 }
139 }
140
141 public virtual bool IsGuest
142 {
143 get
144 {
146 {
147 return false;
148 }
149 if ((object)s_domainRid == null)
150 {
151 s_domainRid = new SecurityIdentifier(IdentifierAuthority.NTAuthority, new int[2] { 32, 546 });
152 }
154 }
155 }
156
157 public virtual bool IsSystem
158 {
159 get
160 {
162 {
163 return false;
164 }
165 if ((object)s_localSystemRid == null)
166 {
167 s_localSystemRid = new SecurityIdentifier(IdentifierAuthority.NTAuthority, new int[1] { 18 });
168 }
169 return User == s_localSystemRid;
170 }
171 }
172
173 public virtual bool IsAnonymous
174 {
175 get
176 {
178 {
179 return true;
180 }
181 if ((object)s_anonymousRid == null)
182 {
183 s_anonymousRid = new SecurityIdentifier(IdentifierAuthority.NTAuthority, new int[1] { 7 });
184 }
185 return User == s_anonymousRid;
186 }
187 }
188
189 public override string Name => GetName();
190
192 {
193 get
194 {
196 {
197 return null;
198 }
199 if (_owner == null)
200 {
203 }
204 return _owner;
205 }
206 }
207
209 {
210 get
211 {
213 {
214 return null;
215 }
216 if (_user == null)
217 {
220 }
221 return _user;
222 }
223 }
224
226 {
227 get
228 {
230 {
231 return null;
232 }
233 if (_groups == null)
234 {
237 {
238 global::Interop.SID_AND_ATTRIBUTES[] array = new global::Interop.SID_AND_ATTRIBUTES[safeLocalAllocHandle.Read<global::Interop.TOKEN_GROUPS>(0uL).GroupCount];
239 safeLocalAllocHandle.ReadArray((uint)Marshal.OffsetOf<global::Interop.TOKEN_GROUPS>("Groups").ToInt32(), array, 0, array.Length);
240 global::Interop.SID_AND_ATTRIBUTES[] array2 = array;
241 for (int i = 0; i < array2.Length; i++)
242 {
243 global::Interop.SID_AND_ATTRIBUTES sID_AND_ATTRIBUTES = array2[i];
244 uint num = 3221225492u;
245 if ((sID_AND_ATTRIBUTES.Attributes & num) == 4)
246 {
248 }
249 }
250 }
252 }
253 return _groups;
254 }
255 }
256
258
259 public virtual IntPtr Token => _safeTokenHandle.DangerousGetHandle();
260
262 {
263 get
264 {
266 return _userClaims.ToArray();
267 }
268 }
269
271 {
272 get
273 {
275 return _deviceClaims.ToArray();
276 }
277 }
278
279 public override IEnumerable<Claim> Claims
280 {
281 get
282 {
284 {
286 }
287 foreach (Claim claim in base.Claims)
288 {
289 yield return claim;
290 }
291 foreach (Claim userClaim in _userClaims)
292 {
293 yield return userClaim;
294 }
295 foreach (Claim deviceClaim in _deviceClaims)
296 {
297 yield return deviceClaim;
298 }
299 }
300 }
301
303 : this(userToken, null, -1)
304 {
305 }
306
308 : this(userToken, type, -1)
309 {
310 }
311
316
321
323 : base(identity, null, GetAuthType(identity), null, null)
324 {
325 bool success = false;
326 try
327 {
329 {
332 {
334 }
335 _authType = identity._authType;
337 }
338 }
339 finally
340 {
341 if (success)
342 {
344 }
345 }
346 }
347
349 : base(null, null, null, "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", "http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid")
350 {
354 }
355
357 : base(null, null, null, "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", "http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid")
358 {
359 }
360
362 : base(null, null, null, "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", "http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid")
363 {
364 checked
365 {
368 byte[] array = new byte[4] { 67, 76, 82, 0 };
369 Unsafe.SkipInit(out global::Interop.SspiCli.TOKEN_SOURCE SourceContext);
370 if (!global::Interop.Advapi32.AllocateLocallyUniqueId(out SourceContext.SourceIdentifier))
371 {
372 throw new SecurityException(new Win32Exception().Message);
373 }
374 SourceContext.SourceName = new byte[8];
375 Buffer.BlockCopy(array, 0, SourceContext.SourceName, 0, array.Length);
376 if (sUserPrincipalName == null)
377 {
378 throw new ArgumentNullException("sUserPrincipalName");
379 }
380 byte[] array2 = Encoding.Unicode.GetBytes(sUserPrincipalName);
381 if (array2.Length > 65535)
382 {
383 Array.Resize(ref array2, array2.Length & 0xFFFF);
384 }
385 int num = sizeof(global::Interop.SspiCli.KERB_S4U_LOGON) + array2.Length;
387 global::Interop.SspiCli.KERB_S4U_LOGON* ptr = unchecked((global::Interop.SspiCli.KERB_S4U_LOGON*)(void*)safeLocalAllocHandle.DangerousGetHandle());
388 ptr->MessageType = global::Interop.SspiCli.KERB_LOGON_SUBMIT_TYPE.KerbS4ULogon;
389 ptr->Flags = global::Interop.SspiCli.KerbS4uLogonFlags.None;
390 ptr->ClientUpn.Length = (ptr->ClientUpn.MaximumLength = (ushort)array2.Length);
391 IntPtr buffer = (IntPtr)(ptr + 1);
392 ptr->ClientUpn.Buffer = buffer;
393 Marshal.Copy(array2, 0, ptr->ClientUpn.Buffer, array2.Length);
394 ptr->ClientRealm.Length = (ptr->ClientRealm.MaximumLength = 0);
395 ptr->ClientRealm.Buffer = IntPtr.Zero;
396 ushort num2 = (ushort)array.Length;
398 Marshal.Copy(array, 0, safeLocalAllocHandle2.DangerousGetHandle(), array.Length);
399 global::Interop.Advapi32.LSA_STRING OriginName = new global::Interop.Advapi32.LSA_STRING(safeLocalAllocHandle2.DangerousGetHandle(), num2);
402 global::Interop.LUID LogonId;
404 global::Interop.SspiCli.QUOTA_LIMITS Quotas;
405 int SubStatus;
406 int num3 = global::Interop.SspiCli.LsaLogonUser(lsaHandle, ref OriginName, global::Interop.SspiCli.SECURITY_LOGON_TYPE.Network, authenticationPackage, safeLocalAllocHandle.DangerousGetHandle(), num, IntPtr.Zero, ref SourceContext, out ProfileBuffer, out ProfileBufferLength, out LogonId, out Token, out Quotas, out SubStatus);
407 if (num3 == -1073741714 && SubStatus < 0)
408 {
409 num3 = SubStatus;
410 }
411 if (num3 < 0)
412 {
414 }
415 if (SubStatus < 0)
416 {
418 }
419 ProfileBuffer?.Dispose();
421 }
422 }
423
425 {
427 int num = global::Interop.SspiCli.LsaConnectUntrusted(out LsaHandle);
428 if (num < 0)
429 {
430 throw GetExceptionFromNtStatus(num);
431 }
432 return LsaHandle;
433 }
434
436 {
437 byte[] bytes = Encoding.ASCII.GetBytes(packageName);
438 int AuthenticationPackage;
439 fixed (byte* ptr = &bytes[0])
440 {
441 global::Interop.Advapi32.LSA_STRING PackageName = new global::Interop.Advapi32.LSA_STRING((IntPtr)ptr, checked((ushort)bytes.Length));
442 int num = global::Interop.SspiCli.LsaLookupAuthenticationPackage(lsaHandle, ref PackageName, out AuthenticationPackage);
443 if (num < 0)
444 {
445 throw GetExceptionFromNtStatus(num);
446 }
447 }
448 return AuthenticationPackage;
449 }
450
452 {
453 if (accessToken == IntPtr.Zero)
454 {
456 }
457 uint ReturnLength = 4u;
458 if (!global::Interop.Advapi32.GetTokenInformation(accessToken, 8u, IntPtr.Zero, 0u, out ReturnLength) && Marshal.GetLastWin32Error() == 6)
459 {
461 }
463 IntPtr currentProcess = global::Interop.Kernel32.GetCurrentProcess();
464 if (!global::Interop.Kernel32.DuplicateHandle(currentProcess, accessToken, currentProcess, ref lpTargetHandle, 0u, bInheritHandle: true, 2u))
465 {
466 throw new SecurityException(new Win32Exception().Message);
467 }
468 return lpTargetHandle;
469 }
470
472 {
473 if (accessToken.IsInvalid)
474 {
475 return accessToken;
476 }
477 bool success = false;
478 try
479 {
480 accessToken.DangerousAddRef(ref success);
481 return DuplicateAccessToken(accessToken.DangerousGetHandle());
482 }
483 finally
484 {
485 if (success)
486 {
487 accessToken.DangerousRelease();
488 }
489 }
490 }
491
496
501
506
508 {
510 }
511
513 {
514 return GetCurrentInternal(TokenAccessLevels.MaximumAllowed, threadOnly: false);
515 }
516
518 {
520 }
521
522 public static WindowsIdentity GetCurrent(TokenAccessLevels desiredAccess)
523 {
524 return GetCurrentInternal(desiredAccess, threadOnly: false);
525 }
526
528 {
529 return new WindowsIdentity();
530 }
531
533 {
535 {
536 return false;
537 }
540 bool IsMember = false;
541 try
542 {
543 if (impersonationLevel == TokenImpersonationLevel.None && !global::Interop.Advapi32.DuplicateTokenEx(_safeTokenHandle, 8u, IntPtr.Zero, 2u, 2u, ref phNewToken))
544 {
545 throw new SecurityException(new Win32Exception().Message);
546 }
547 if (!global::Interop.Advapi32.CheckTokenMembership((impersonationLevel != 0) ? _safeTokenHandle : phNewToken, sid.BinaryForm, ref IsMember))
548 {
549 throw new SecurityException(new Win32Exception().Message);
550 }
551 return IsMember;
552 }
553 finally
554 {
556 {
557 phNewToken.Dispose();
558 }
559 }
560 }
561
562 internal string GetName()
563 {
565 {
566 return string.Empty;
567 }
568 if (_name == null)
569 {
571 {
572 NTAccount nTAccount = User.Translate(typeof(NTAccount)) as NTAccount;
573 _name = nTAccount.ToString();
574 });
575 }
576 return _name;
577 }
578
580 {
581 if (action == null)
582 {
583 throw new ArgumentNullException("action");
584 }
586 }
587
589 {
590 Func<T> func2 = func;
591 if (func2 == null)
592 {
593 throw new ArgumentNullException("func");
594 }
595 T result = default(T);
597 {
598 result = func2();
599 });
600 return result;
601 }
602
607
612
613 protected virtual void Dispose(bool disposing)
614 {
616 {
618 }
619 _name = null;
620 _owner = null;
621 _user = null;
622 }
623
624 public void Dispose()
625 {
626 Dispose(disposing: true);
627 }
628
629 private static void RunImpersonatedInternal(SafeAccessTokenHandle token, Action action)
630 {
631 token = DuplicateAccessToken(token);
632 bool isImpersonating;
633 int hr;
634 SafeAccessTokenHandle currentToken = GetCurrentToken(TokenAccessLevels.MaximumAllowed, threadOnly: false, out isImpersonating, out hr);
635 if (currentToken == null || currentToken.IsInvalid)
636 {
637 throw new SecurityException(new Win32Exception(hr).Message);
638 }
639 s_currentImpersonatedToken.Value = (isImpersonating ? currentToken : null);
642 {
643 if (!global::Interop.Advapi32.RevertToSelf())
644 {
645 Environment.FailFast(new Win32Exception().Message);
646 }
648 if (!token.IsInvalid && !global::Interop.Advapi32.ImpersonateLoggedOnUser(token))
649 {
651 }
653 action();
654 }, null);
655 }
656
658 {
659 if (args.ThreadContextChanged)
660 {
661 if (!global::Interop.Advapi32.RevertToSelf())
662 {
663 Environment.FailFast(new Win32Exception().Message);
664 }
665 if (args.CurrentValue != null && !args.CurrentValue.IsInvalid && !global::Interop.Advapi32.ImpersonateLoggedOnUser(args.CurrentValue))
666 {
667 Environment.FailFast(new Win32Exception().Message);
668 }
669 }
670 }
671
673 {
674 bool isImpersonating;
675 int hr;
676 SafeAccessTokenHandle currentToken = GetCurrentToken(desiredAccess, threadOnly, out isImpersonating, out hr);
677 if (currentToken == null || currentToken.IsInvalid)
678 {
679 if (threadOnly && !isImpersonating)
680 {
681 return null;
682 }
683 throw new SecurityException(new Win32Exception(hr).Message);
684 }
686 windowsIdentity._safeTokenHandle.Dispose();
688 return windowsIdentity;
689 }
690
691 private static int GetHRForWin32Error(int dwLastError)
692 {
693 if ((dwLastError & 0x80000000u) == 2147483648u)
694 {
695 return dwLastError;
696 }
697 return (dwLastError & 0xFFFF) | -2147024896;
698 }
699
700 private static Exception GetExceptionFromNtStatus(int status)
701 {
702 switch (status)
703 {
704 case -1073741790:
705 return new UnauthorizedAccessException();
706 case -1073741801:
707 case -1073741670:
708 return new OutOfMemoryException();
709 default:
710 {
711 uint error = global::Interop.Advapi32.LsaNtStatusToWinError((uint)status);
712 return new SecurityException(new Win32Exception((int)error).Message);
713 }
714 }
715 }
716
717 private static SafeAccessTokenHandle GetCurrentToken(TokenAccessLevels desiredAccess, bool threadOnly, out bool isImpersonating, out int hr)
718 {
719 isImpersonating = true;
720 hr = 0;
722 bool flag = global::Interop.Advapi32.OpenThreadToken(desiredAccess, WinSecurityContext.Both, out tokenHandle);
723 if (!flag)
724 {
726 }
727 if (!flag && hr == GetHRForWin32Error(1008))
728 {
729 isImpersonating = false;
730 if (!threadOnly)
731 {
732 return GetCurrentProcessToken(desiredAccess, out hr);
733 }
734 }
735 return tokenHandle;
736 }
737
739 {
740 hr = 0;
741 if (!global::Interop.Advapi32.OpenProcessToken(global::Interop.Kernel32.GetCurrentProcess(), desiredAccess, out var TokenHandle))
742 {
744 }
745 return TokenHandle;
746 }
747
753
754 private static global::Interop.LUID GetLogonAuthId(SafeAccessTokenHandle safeTokenHandle)
755 {
757 return safeLocalAllocHandle.Read<global::Interop.TOKEN_STATISTICS>(0uL).AuthenticationId;
758 }
759
761 {
763 uint ReturnLength = 4u;
764 bool tokenInformation = global::Interop.Advapi32.GetTokenInformation(tokenHandle, (uint)tokenInformationClass, invalidHandle, 0u, out ReturnLength);
766 switch (lastWin32Error)
767 {
768 case 24:
769 case 122:
770 invalidHandle.Dispose();
772 if (!global::Interop.Advapi32.GetTokenInformation(tokenHandle, (uint)tokenInformationClass, invalidHandle, ReturnLength, out ReturnLength))
773 {
774 throw new SecurityException(new Win32Exception().Message);
775 }
776 return invalidHandle;
777 case 6:
779 case 87:
781 {
782 invalidHandle.Dispose();
783 return null;
784 }
785 break;
786 }
787 throw new SecurityException(new Win32Exception(lastWin32Error).Message);
788 }
789
790 private static string GetAuthType(WindowsIdentity identity)
791 {
792 if (identity == null)
793 {
794 throw new ArgumentNullException("identity");
795 }
796 return identity._authType;
797 }
798
799 public override ClaimsIdentity Clone()
800 {
801 return new WindowsIdentity(this);
802 }
803
804 private void InitializeClaims()
805 {
806 bool target = false;
808 {
809 _userClaims = new List<Claim>();
811 if (!string.IsNullOrEmpty(Name))
812 {
813 _userClaims.Add(new Claim(base.NameClaimType, Name, "http://www.w3.org/2001/XMLSchema#string", _issuerName, _issuerName, this));
814 }
818 {
821 {
822 AddTokenClaims(_userClaims, TokenInformationClass.TokenUserClaimAttributes, "http://schemas.microsoft.com/ws/2008/06/identity/claims/windowsuserclaim");
823 AddTokenClaims(_deviceClaims, TokenInformationClass.TokenDeviceClaimAttributes, "http://schemas.microsoft.com/ws/2008/06/identity/claims/windowsdeviceclaim");
824 }
825 }
826 return true;
827 });
828 }
829
831 {
833 {
834 return;
835 }
838 try
839 {
841 SecurityIdentifier securityIdentifier = new SecurityIdentifier(Marshal.PtrToStructure<global::Interop.TOKEN_PRIMARY_GROUP>(safeLocalAllocHandle2.DangerousGetHandle()).PrimaryGroup);
842 bool flag = false;
844 int num = Marshal.ReadInt32(safeLocalAllocHandle.DangerousGetHandle());
845 IntPtr intPtr = new IntPtr((long)safeLocalAllocHandle.DangerousGetHandle() + (long)Marshal.OffsetOf<global::Interop.TOKEN_GROUPS>("Groups"));
846 for (int i = 0; i < num; i++)
847 {
848 global::Interop.SID_AND_ATTRIBUTES sID_AND_ATTRIBUTES = Marshal.PtrToStructure<global::Interop.SID_AND_ATTRIBUTES>(intPtr);
849 uint num2 = 3221225492u;
851 if ((sID_AND_ATTRIBUTES.Attributes & num2) == 4)
852 {
853 Claim claim;
854 if (!flag && StringComparer.Ordinal.Equals(securityIdentifier2.Value, securityIdentifier.Value))
855 {
856 claim = new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/primarygroupsid", securityIdentifier2.Value, "http://www.w3.org/2001/XMLSchema#string", _issuerName, _issuerName, this);
857 claim.Properties.Add("http://schemas.microsoft.com/ws/2008/06/identity/claims/windowssubauthority", securityIdentifier2.IdentifierAuthority.ToString());
859 flag = true;
860 }
861 claim = new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid", securityIdentifier2.Value, "http://www.w3.org/2001/XMLSchema#string", _issuerName, _issuerName, this);
862 claim.Properties.Add("http://schemas.microsoft.com/ws/2008/06/identity/claims/windowssubauthority", securityIdentifier2.IdentifierAuthority.ToString());
864 }
865 else if ((sID_AND_ATTRIBUTES.Attributes & num2) == 16)
866 {
867 Claim claim;
868 if (!flag && StringComparer.Ordinal.Equals(securityIdentifier2.Value, securityIdentifier.Value))
869 {
870 claim = new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/denyonlyprimarygroupsid", securityIdentifier2.Value, "http://www.w3.org/2001/XMLSchema#string", _issuerName, _issuerName, this);
871 claim.Properties.Add("http://schemas.microsoft.com/ws/2008/06/identity/claims/windowssubauthority", securityIdentifier2.IdentifierAuthority.ToString());
873 flag = true;
874 }
875 claim = new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/denyonlysid", securityIdentifier2.Value, "http://www.w3.org/2001/XMLSchema#string", _issuerName, _issuerName, this);
876 claim.Properties.Add("http://schemas.microsoft.com/ws/2008/06/identity/claims/windowssubauthority", securityIdentifier2.IdentifierAuthority.ToString());
878 }
879 intPtr = new IntPtr((long)intPtr + Marshal.SizeOf<global::Interop.SID_AND_ATTRIBUTES>());
880 }
881 }
882 finally
883 {
884 safeLocalAllocHandle?.Dispose();
885 safeLocalAllocHandle2?.Dispose();
886 }
887 }
888
890 {
892 {
893 return;
894 }
896 try
897 {
899 global::Interop.SID_AND_ATTRIBUTES sID_AND_ATTRIBUTES = Marshal.PtrToStructure<global::Interop.SID_AND_ATTRIBUTES>(safeLocalAllocHandle.DangerousGetHandle());
900 uint num = 16u;
902 if (sID_AND_ATTRIBUTES.Attributes == 0)
903 {
904 Claim claim = new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/primarysid", securityIdentifier.Value, "http://www.w3.org/2001/XMLSchema#string", _issuerName, _issuerName, this);
905 claim.Properties.Add("http://schemas.microsoft.com/ws/2008/06/identity/claims/windowssubauthority", securityIdentifier.IdentifierAuthority.ToString());
907 }
908 else if ((sID_AND_ATTRIBUTES.Attributes & num) == 16)
909 {
910 Claim claim = new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/denyonlyprimarysid", securityIdentifier.Value, "http://www.w3.org/2001/XMLSchema#string", _issuerName, _issuerName, this);
911 claim.Properties.Add("http://schemas.microsoft.com/ws/2008/06/identity/claims/windowssubauthority", securityIdentifier.IdentifierAuthority.ToString());
913 }
914 }
915 finally
916 {
917 safeLocalAllocHandle?.Dispose();
918 }
919 }
920
922 {
924 {
925 return;
926 }
928 try
929 {
931 if (safeLocalAllocHandle == null)
932 {
934 return;
935 }
936 int num = Marshal.ReadInt32(safeLocalAllocHandle.DangerousGetHandle());
937 IntPtr intPtr = new IntPtr((long)safeLocalAllocHandle.DangerousGetHandle() + (long)Marshal.OffsetOf(typeof(global::Interop.TOKEN_GROUPS), "Groups"));
938 for (int i = 0; i < num; i++)
939 {
940 global::Interop.SID_AND_ATTRIBUTES sID_AND_ATTRIBUTES = (global::Interop.SID_AND_ATTRIBUTES)Marshal.PtrToStructure(intPtr, typeof(global::Interop.SID_AND_ATTRIBUTES));
941 uint num2 = 3221225492u;
943 if ((sID_AND_ATTRIBUTES.Attributes & num2) == 4)
944 {
945 string text = "http://schemas.microsoft.com/ws/2008/06/identity/claims/windowsdevicegroup";
946 Claim claim = new Claim(text, securityIdentifier.Value, "http://www.w3.org/2001/XMLSchema#string", _issuerName, _issuerName, this);
947 claim.Properties.Add("http://schemas.microsoft.com/ws/2008/06/identity/claims/windowssubauthority", securityIdentifier.IdentifierAuthority.ToString());
948 claim.Properties.Add(text, "");
950 }
951 else if ((sID_AND_ATTRIBUTES.Attributes & num2) == 16)
952 {
953 string text = "http://schemas.microsoft.com/ws/2008/06/identity/claims/denyonlywindowsdevicegroup";
954 Claim claim2 = new Claim(text, securityIdentifier.Value, "http://www.w3.org/2001/XMLSchema#string", _issuerName, _issuerName, this);
955 claim2.Properties.Add("http://schemas.microsoft.com/ws/2008/06/identity/claims/windowssubauthority", securityIdentifier.IdentifierAuthority.ToString());
956 claim2.Properties.Add(text, "");
958 }
959 intPtr = new IntPtr((long)intPtr + Marshal.SizeOf<global::Interop.SID_AND_ATTRIBUTES>());
960 }
961 }
962 finally
963 {
964 safeLocalAllocHandle?.Dispose();
965 }
966 }
967
969 {
971 {
972 return;
973 }
975 try
976 {
978 global::Interop.CLAIM_SECURITY_ATTRIBUTES_INFORMATION cLAIM_SECURITY_ATTRIBUTES_INFORMATION = (global::Interop.CLAIM_SECURITY_ATTRIBUTES_INFORMATION)Marshal.PtrToStructure(safeLocalAllocHandle.DangerousGetHandle(), typeof(global::Interop.CLAIM_SECURITY_ATTRIBUTES_INFORMATION));
979 long num = 0L;
980 for (int i = 0; i < cLAIM_SECURITY_ATTRIBUTES_INFORMATION.AttributeCount; i++)
981 {
982 IntPtr ptr = new IntPtr(cLAIM_SECURITY_ATTRIBUTES_INFORMATION.Attribute.pAttributeV1.ToInt64() + num);
983 global::Interop.CLAIM_SECURITY_ATTRIBUTE_V1 structure = (global::Interop.CLAIM_SECURITY_ATTRIBUTE_V1)Marshal.PtrToStructure(ptr, typeof(global::Interop.CLAIM_SECURITY_ATTRIBUTE_V1));
984 switch (structure.ValueType)
985 {
986 case global::Interop.ClaimSecurityAttributeType.CLAIM_SECURITY_ATTRIBUTE_TYPE_STRING:
987 {
988 IntPtr[] array4 = new IntPtr[structure.ValueCount];
989 Marshal.Copy(structure.Values.ppString, array4, 0, (int)structure.ValueCount);
990 for (int m = 0; m < structure.ValueCount; m++)
991 {
992 Claim claim4 = new Claim(structure.Name, Marshal.PtrToStringAuto(array4[m]), "http://www.w3.org/2001/XMLSchema#string", _issuerName, _issuerName, this);
993 claim4.Properties.Add(propertyValue, string.Empty);
995 }
996 break;
997 }
998 case global::Interop.ClaimSecurityAttributeType.CLAIM_SECURITY_ATTRIBUTE_TYPE_INT64:
999 {
1000 long[] array2 = new long[structure.ValueCount];
1001 Marshal.Copy(structure.Values.pInt64, array2, 0, (int)structure.ValueCount);
1002 for (int k = 0; k < structure.ValueCount; k++)
1003 {
1004 Claim claim2 = new Claim(structure.Name, array2[k].ToString(CultureInfo.InvariantCulture), "http://www.w3.org/2001/XMLSchema#integer64", _issuerName, _issuerName, this);
1005 claim2.Properties.Add(propertyValue, string.Empty);
1007 }
1008 break;
1009 }
1010 case global::Interop.ClaimSecurityAttributeType.CLAIM_SECURITY_ATTRIBUTE_TYPE_UINT64:
1011 {
1012 long[] array3 = new long[structure.ValueCount];
1013 Marshal.Copy(structure.Values.pUint64, array3, 0, (int)structure.ValueCount);
1014 for (int l = 0; l < structure.ValueCount; l++)
1015 {
1016 string name = structure.Name;
1017 ulong num2 = (ulong)array3[l];
1018 Claim claim3 = new Claim(name, num2.ToString(CultureInfo.InvariantCulture), "http://www.w3.org/2001/XMLSchema#uinteger64", _issuerName, _issuerName, this);
1019 claim3.Properties.Add(propertyValue, string.Empty);
1021 }
1022 break;
1023 }
1024 case global::Interop.ClaimSecurityAttributeType.CLAIM_SECURITY_ATTRIBUTE_TYPE_BOOLEAN:
1025 {
1026 long[] array = new long[structure.ValueCount];
1027 Marshal.Copy(structure.Values.pUint64, array, 0, (int)structure.ValueCount);
1028 for (int j = 0; j < structure.ValueCount; j++)
1029 {
1030 Claim claim = new Claim(structure.Name, (array[j] != 0).ToString(), "http://www.w3.org/2001/XMLSchema#boolean", _issuerName, _issuerName, this);
1031 claim.Properties.Add(propertyValue, string.Empty);
1033 }
1034 break;
1035 }
1036 }
1037 num += Marshal.SizeOf(structure);
1038 }
1039 }
1040 finally
1041 {
1042 safeLocalAllocHandle?.Dispose();
1043 }
1044 }
1045}
static Microsoft.Win32.SafeHandles.SafeLocalAllocHandle LocalAlloc(int cb)
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
void Add(TKey key, TValue value)
static void FailFast(string? message)
static CultureInfo InvariantCulture
static ? string PtrToStringAuto(IntPtr ptr, int len)
Definition Marshal.cs:1572
static int ReadInt32(object ptr, int ofs)
Definition Marshal.cs:59
static unsafe? string PtrToStringUni(IntPtr ptr)
Definition Marshal.cs:652
static void Copy(int[] source, int startIndex, IntPtr destination, int length)
Definition Marshal.cs:800
static ? object PtrToStructure(IntPtr ptr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type structureType)
Definition Marshal.cs:1164
static int SizeOf(object structure)
Definition Marshal.cs:697
static IntPtr OffsetOf(Type t, string fieldName)
Definition Marshal.cs:28
void DangerousAddRef(ref bool success)
Definition SafeHandle.cs:76
static string Argument_TokenZero
Definition SR.cs:26
static string Argument_ImpersonateUser
Definition SR.cs:18
static string Argument_InvalidImpersonationToken
Definition SR.cs:22
Definition SR.cs:7
static SafeAccessTokenHandle GetCurrentToken(TokenAccessLevels desiredAccess, bool threadOnly, out bool isImpersonating, out int hr)
static SecurityIdentifier s_authenticatedUserRid
void AddPrimarySidClaim(List< Claim > instanceClaims)
unsafe WindowsIdentity(string sUserPrincipalName)
volatile TokenImpersonationLevel _impersonationLevel
static void RunImpersonatedInternal(SafeAccessTokenHandle token, Action action)
static T RunImpersonated< T >(SafeAccessTokenHandle safeAccessTokenHandle, Func< T > func)
static readonly AsyncLocal< SafeAccessTokenHandle > s_currentImpersonatedToken
static SecurityIdentifier s_localSystemRid
WindowsIdentity(IntPtr userToken, string authType, int isAuthenticated)
bool CheckNtTokenForSid(SecurityIdentifier sid)
static Task< T > RunImpersonatedAsync< T >(SafeAccessTokenHandle safeAccessTokenHandle, Func< Task< T > > func)
static int GetHRForWin32Error(int dwLastError)
static SafeLocalAllocHandle GetTokenInformation(SafeAccessTokenHandle tokenHandle, TokenInformationClass tokenInformationClass, bool nullOnInvalidParam=false)
void AddTokenClaims(List< Claim > instanceClaims, TokenInformationClass tokenInformationClass, string propertyValue)
virtual IEnumerable< Claim > UserClaims
WindowsIdentity(IntPtr userToken, string type, WindowsAccountType acctType, bool isAuthenticated)
static WindowsIdentity GetCurrentInternal(TokenAccessLevels desiredAccess, bool threadOnly)
WindowsIdentity(IntPtr userToken, string type, WindowsAccountType acctType)
virtual IEnumerable< Claim > DeviceClaims
static void RunImpersonated(SafeAccessTokenHandle safeAccessTokenHandle, Action action)
static Exception GetExceptionFromNtStatus(int status)
static ? WindowsIdentity GetCurrent(bool ifImpersonating)
static SafeAccessTokenHandle DuplicateAccessToken(IntPtr accessToken)
static global::Interop.LUID GetLogonAuthId(SafeAccessTokenHandle safeTokenHandle)
T GetTokenInformation< T >(TokenInformationClass tokenInformationClass)
static void CurrentImpersonatedTokenChanged(AsyncLocalValueChangedArgs< SafeAccessTokenHandle > args)
static SafeAccessTokenHandle GetCurrentProcessToken(TokenAccessLevels desiredAccess, out int hr)
WindowsIdentity(IntPtr userToken, string type)
static unsafe int LookupAuthenticationPackage(SafeLsaHandle lsaHandle, string packageName)
WindowsIdentity(SerializationInfo info, StreamingContext context)
static Task RunImpersonatedAsync(SafeAccessTokenHandle safeAccessTokenHandle, Func< Task > func)
static WindowsIdentity GetCurrent(TokenAccessLevels desiredAccess)
static string GetAuthType(WindowsIdentity identity)
void AddGroupSidClaims(List< Claim > instanceClaims)
static SafeAccessTokenHandle DuplicateAccessToken(SafeAccessTokenHandle accessToken)
void AddDeviceGroupSidClaims(List< Claim > instanceClaims, TokenInformationClass tokenInformationClass)
static StringComparer Ordinal
static Encoding Unicode
Definition Encoding.cs:519
static Encoding ASCII
Definition Encoding.cs:511
static ? ExecutionContext Capture()
static void Run(ExecutionContext executionContext, ContextCallback callback, object? state)
static int CompareExchange(ref int location1, int value, int comparand)
void GetObjectData(SerializationInfo info, StreamingContext context)
static readonly IntPtr Zero
Definition IntPtr.cs:18