mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Consolidate GetUserName code
Once .NET Core 1.1 becomes available, we can likely remove the entirety of CorePsExtensions.cs.
This commit is contained in:
@@ -1026,25 +1026,8 @@ namespace System.Management.Automation
|
||||
{
|
||||
return Platform.NonWindowsGetDomainName();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static string WinGetUserName()
|
||||
{
|
||||
StringBuilder domainName = new StringBuilder(1024);
|
||||
uint domainNameLen = (uint)domainName.Capacity;
|
||||
|
||||
byte ret = Win32Native.GetUserNameEx(Win32Native.NameSamCompatible, domainName, ref domainNameLen);
|
||||
if (ret == 1)
|
||||
{
|
||||
string samName = domainName.ToString();
|
||||
int index = samName.IndexOf('\\');
|
||||
if (index != -1)
|
||||
{
|
||||
return samName.Substring(index + 1);
|
||||
}
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1054,14 +1037,24 @@ namespace System.Management.Automation
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Platform.IsWindows)
|
||||
#if UNIX
|
||||
return Platform.Unix.UserName;
|
||||
#else
|
||||
StringBuilder domainName = new StringBuilder(1024);
|
||||
uint domainNameLen = (uint)domainName.Capacity;
|
||||
|
||||
byte ret = Win32Native.GetUserNameEx(Win32Native.NameSamCompatible, domainName, ref domainNameLen);
|
||||
if (ret == 1)
|
||||
{
|
||||
return WinGetUserName();
|
||||
}
|
||||
else
|
||||
{
|
||||
return Platform.NonWindowsGetUserName();
|
||||
string samName = domainName.ToString();
|
||||
int index = samName.IndexOf('\\');
|
||||
if (index != -1)
|
||||
{
|
||||
return samName.Substring(index + 1);
|
||||
}
|
||||
}
|
||||
return string.Empty;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -314,11 +314,6 @@ namespace System.Management.Automation
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
internal static string NonWindowsGetUserName()
|
||||
{
|
||||
return Unix.UserName;
|
||||
}
|
||||
|
||||
// Hostname in this context seems to be the FQDN
|
||||
internal static string NonWindowsGetHostName()
|
||||
{
|
||||
|
||||
@@ -6,6 +6,10 @@ using Dbg = System.Management.Automation.Diagnostics;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Collections.Generic;
|
||||
|
||||
#if CORECLR
|
||||
using Environment = System.Management.Automation.Environment;
|
||||
#endif
|
||||
|
||||
namespace System.Management.Automation
|
||||
{
|
||||
/// <summary>
|
||||
@@ -38,15 +42,12 @@ namespace System.Management.Automation
|
||||
|
||||
this.TimeGenerated = DateTime.Now;
|
||||
this.Tags = new List<string>();
|
||||
if (Platform.IsWindows)
|
||||
{
|
||||
this.User = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.User = Platform.NonWindowsGetUserName();
|
||||
}
|
||||
// Porting note: PsUtils.GetHostName() already handles platform specifics
|
||||
// domain\user on Windows, just user on Unix
|
||||
#if UNIX
|
||||
this.User = Platform.Unix.UserName;
|
||||
#else
|
||||
this.User = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
|
||||
#endif
|
||||
this.Computer = PsUtils.GetHostName();
|
||||
this.ProcessId = (uint)System.Diagnostics.Process.GetCurrentProcess().Id;
|
||||
this.NativeThreadId = PsUtils.GetNativeThreadId();
|
||||
|
||||
@@ -3148,7 +3148,13 @@ namespace System.Management.Automation.Runspaces
|
||||
// otherwise use the current user name.
|
||||
var userName = (!string.IsNullOrEmpty(this.UserDriveUserName)) ?
|
||||
this.UserDriveUserName :
|
||||
System.Security.Principal.WindowsIdentity.GetCurrent().Name;
|
||||
// domain\user on Windows, just user on Unix
|
||||
#if UNIX
|
||||
Platform.Unix.UserName
|
||||
#else
|
||||
System.Security.Principal.WindowsIdentity.GetCurrent().Name
|
||||
#endif
|
||||
;
|
||||
|
||||
// Ensure that user name contains no invalid path characters.
|
||||
// MSDN indicates that logon names cannot contain any of these invalid characters,
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace PSTests
|
||||
// The process should return an exit code of 0 on success
|
||||
Assert.Equal(0, process.ExitCode);
|
||||
// It should be the same as what our platform code returns
|
||||
Assert.Equal(username, Platform.NonWindowsGetUserName());
|
||||
Assert.Equal(username, Platform.Unix.UserName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user