| | | 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.Runtime.InteropServices; |
| | | 5 | | using System.Text; |
| | | 6 | | |
| | | 7 | | internal static partial class Interop |
| | | 8 | | { |
| | | 9 | | internal static partial class Secur32 |
| | | 10 | | { |
| | | 11 | | internal static bool GetCurrentUpn(out string upn) |
| | | 12 | | { |
| | 0 | 13 | | if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| | | 14 | | { |
| | 0 | 15 | | upn = null; |
| | 0 | 16 | | return false; |
| | | 17 | | } |
| | | 18 | | |
| | 0 | 19 | | StringBuilder sb = new StringBuilder(1024); |
| | 0 | 20 | | uint upnNameLength = (uint)sb.Capacity; |
| | | 21 | | |
| | 0 | 22 | | if (GetUserNameEx(EXTENDED_NAME_FORMAT.NameUserPrincipal, sb, ref upnNameLength) == BOOLEAN.FALSE) |
| | | 23 | | { |
| | 0 | 24 | | int error = Marshal.GetLastWin32Error(); |
| | 0 | 25 | | if (error == ERROR_MORE_DATA) |
| | | 26 | | { |
| | 0 | 27 | | sb.Capacity = (int)++upnNameLength; |
| | 0 | 28 | | if (GetUserNameEx(EXTENDED_NAME_FORMAT.NameUserPrincipal, sb, ref upnNameLength) == BOOLEAN.FALSE) |
| | | 29 | | { |
| | 0 | 30 | | upn = null; |
| | 0 | 31 | | return false; |
| | | 32 | | } |
| | | 33 | | } |
| | | 34 | | else |
| | | 35 | | { |
| | 0 | 36 | | upn = null; |
| | 0 | 37 | | return false; |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | upn = sb.ToString(); |
| | 0 | 42 | | return true; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | internal const int ERROR_MORE_DATA = 0xEA; |
| | | 46 | | |
| | | 47 | | [DllImport("secur32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
| | | 48 | | internal static extern BOOLEAN GetUserNameEx(EXTENDED_NAME_FORMAT nameFormat, [Out] StringBuilder lpNameBuffer, |
| | | 49 | | } |
| | | 50 | | } |