mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 04:36:39 +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:
1 parent
3f9889d6da
commit
a8416d7b30
5 files changed
+34
-39
No files matched your search
@@ -1026,25 +1026,8 @@ namespace System.Management.Automation
|
|||||||
{
|
{
|
||||||
return Platform.NonWindowsGetDomainName();
|
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>
|
/// <summary>
|
||||||
@@ -1054,14 +1037,24 @@ namespace System.Management.Automation
|
|||||||
{
|
{
|
||||||
get
|
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();
|
string samName = domainName.ToString();
|
||||||
}
|
int index = samName.IndexOf('\\');
|
||||||
else
|
if (index != -1)
|
||||||
{
|
{
|
||||||
return Platform.NonWindowsGetUserName();
|
return samName.Substring(index + 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return string.Empty;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -314,11 +314,6 @@ namespace System.Management.Automation
|
|||||||
return string.Empty;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string NonWindowsGetUserName()
|
|
||||||
{
|
|
||||||
return Unix.UserName;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hostname in this context seems to be the FQDN
|
// Hostname in this context seems to be the FQDN
|
||||||
internal static string NonWindowsGetHostName()
|
internal static string NonWindowsGetHostName()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ using Dbg = System.Management.Automation.Diagnostics;
|
|||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
#if CORECLR
|
||||||
|
using Environment = System.Management.Automation.Environment;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace System.Management.Automation
|
namespace System.Management.Automation
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -38,15 +42,12 @@ namespace System.Management.Automation
|
|||||||
|
|
||||||
this.TimeGenerated = DateTime.Now;
|
this.TimeGenerated = DateTime.Now;
|
||||||
this.Tags = new List<string>();
|
this.Tags = new List<string>();
|
||||||
if (Platform.IsWindows)
|
// domain\user on Windows, just user on Unix
|
||||||
{
|
#if UNIX
|
||||||
this.User = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
|
this.User = Platform.Unix.UserName;
|
||||||
}
|
#else
|
||||||
else
|
this.User = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
|
||||||
{
|
#endif
|
||||||
this.User = Platform.NonWindowsGetUserName();
|
|
||||||
}
|
|
||||||
// Porting note: PsUtils.GetHostName() already handles platform specifics
|
|
||||||
this.Computer = PsUtils.GetHostName();
|
this.Computer = PsUtils.GetHostName();
|
||||||
this.ProcessId = (uint)System.Diagnostics.Process.GetCurrentProcess().Id;
|
this.ProcessId = (uint)System.Diagnostics.Process.GetCurrentProcess().Id;
|
||||||
this.NativeThreadId = PsUtils.GetNativeThreadId();
|
this.NativeThreadId = PsUtils.GetNativeThreadId();
|
||||||
|
|||||||
@@ -3148,7 +3148,13 @@ namespace System.Management.Automation.Runspaces
|
|||||||
// otherwise use the current user name.
|
// otherwise use the current user name.
|
||||||
var userName = (!string.IsNullOrEmpty(this.UserDriveUserName)) ?
|
var userName = (!string.IsNullOrEmpty(this.UserDriveUserName)) ?
|
||||||
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.
|
// Ensure that user name contains no invalid path characters.
|
||||||
// MSDN indicates that logon names cannot contain any of these invalid 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
|
// The process should return an exit code of 0 on success
|
||||||
Assert.Equal(0, process.ExitCode);
|
Assert.Equal(0, process.ExitCode);
|
||||||
// It should be the same as what our platform code returns
|
// 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