void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { // TODO: Add back when ClaimsIdentity is serializable // base.GetObjectData(info, context); info.AddValue("m_userToken", _safeTokenHandle.DangerousGetHandle()); }
private void CreateUser() { string testAccountPassword; using (RandomNumberGenerator rng = new RNGCryptoServiceProvider()) { byte[] randomBytes = new byte[33]; rng.GetBytes(randomBytes); // Add special chars to ensure it satisfies password requirements. 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 }; // 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 { 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); } } 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(); } } } }
internal IntPtr GetTokenInternal() { return(_safeTokenHandle.DangerousGetHandle()); }