diff --git a/src/libraries/System.Security.Principal.Windows/tests/WindowsIdentityImpersonatedTests.netcoreapp.cs b/src/libraries/System.Security.Principal.Windows/tests/WindowsIdentityImpersonatedTests.netcoreapp.cs index 868afcc8c1a4f..b47921dfa8497 100644 --- a/src/libraries/System.Security.Principal.Windows/tests/WindowsIdentityImpersonatedTests.netcoreapp.cs +++ b/src/libraries/System.Security.Principal.Windows/tests/WindowsIdentityImpersonatedTests.netcoreapp.cs @@ -127,62 +127,57 @@ public WindowsTestAccount(string userName) private void CreateUser() { - string testAccountPassword; - using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) - { - byte[] randomBytes = new byte[33]; - rng.GetBytes(randomBytes); + byte[] randomBytes = RandomNumberGenerator.GetBytes(33); - // Add special chars to ensure it satisfies password requirements. - testAccountPassword = Convert.ToBase64String(randomBytes) + "_-As@!%*(1)4#2"; + // Add special chars to ensure it satisfies password requirements. + string testAccountPassword = Convert.ToBase64String(randomBytes) + "_-As@!%*(1)4#2"; - USER_INFO_1 userInfo = new USER_INFO_1 - { - usri1_name = _userName, - usri1_password = testAccountPassword, - usri1_priv = 1 - }; + USER_INFO_1 userInfo = new USER_INFO_1 + { + usri1_name = _userName, + usri1_password = testAccountPassword, + usri1_priv = 1 + }; - // Create user and remove/create if already exists - uint result = NetUserAdd(null, 1, ref userInfo, out uint param_err); + // Create user and remove/create if already exists + uint result = NetUserAdd(null, 1, ref userInfo, out uint param_err); - // error codes https://docs.microsoft.com/en-us/windows/desktop/netmgmt/network-management-error-codes - // 0 == NERR_Success - if (result == 2224) // NERR_UserExists + // error codes https://docs.microsoft.com/en-us/windows/desktop/netmgmt/network-management-error-codes + // 0 == NERR_Success + if (result == 2224) // NERR_UserExists + { + result = NetUserDel(null, userInfo.usri1_name); + if (result != 0) { - result = NetUserDel(null, userInfo.usri1_name); - if (result != 0) - { - throw new Win32Exception((int)result); - } - result = NetUserAdd(null, 1, ref userInfo, out param_err); - if (result != 0) - { - throw new Win32Exception((int)result); - } + throw new Win32Exception((int)result); } - - const int LOGON32_PROVIDER_DEFAULT = 0; - const int LOGON32_LOGON_INTERACTIVE = 2; - - if (!LogonUser(_userName, ".", testAccountPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out _accountTokenHandle)) + result = NetUserAdd(null, 1, ref userInfo, out param_err); + if (result != 0) { - _accountTokenHandle = null; - throw new Exception($"Failed to get SafeAccessTokenHandle for test account {_userName}", new Win32Exception()); + throw new Win32Exception((int)result); } + } - bool gotRef = false; - try - { - _accountTokenHandle.DangerousAddRef(ref gotRef); - IntPtr logonToken = _accountTokenHandle.DangerousGetHandle(); - AccountName = new WindowsIdentity(logonToken).Name; - } - finally - { - if (gotRef) - _accountTokenHandle.DangerousRelease(); - } + const int LOGON32_PROVIDER_DEFAULT = 0; + const int LOGON32_LOGON_INTERACTIVE = 2; + + if (!LogonUser(_userName, ".", testAccountPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out _accountTokenHandle)) + { + _accountTokenHandle = null; + throw new Exception($"Failed to get SafeAccessTokenHandle for test account {_userName}", new Win32Exception()); + } + + bool gotRef = false; + try + { + _accountTokenHandle.DangerousAddRef(ref gotRef); + IntPtr logonToken = _accountTokenHandle.DangerousGetHandle(); + AccountName = new WindowsIdentity(logonToken).Name; + } + finally + { + if (gotRef) + _accountTokenHandle.DangerousRelease(); } }