2019-01-02 22:48:56 +00:00
|
|
|
|
namespace DSInternals.Common
|
2015-12-26 22:44:43 +00:00
|
|
|
|
{
|
2019-01-02 22:48:56 +00:00
|
|
|
|
using DSInternals.Common.Interop;
|
|
|
|
|
using System.Security;
|
|
|
|
|
using System.Security.AccessControl;
|
|
|
|
|
|
2015-12-26 22:44:43 +00:00
|
|
|
|
public static class StringExtensions
|
|
|
|
|
{
|
|
|
|
|
public static string TrimEnd(this string input, string suffix)
|
|
|
|
|
{
|
|
|
|
|
if(! string.IsNullOrEmpty(input) && ! string.IsNullOrEmpty(suffix) && input.EndsWith(suffix))
|
|
|
|
|
{
|
|
|
|
|
int trimmedLength = input.Length-suffix.Length;
|
|
|
|
|
return input.Remove(trimmedLength);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-01 14:24:41 +00:00
|
|
|
|
|
2015-12-26 22:44:43 +00:00
|
|
|
|
public static SecureString ToSecureString(this string input)
|
|
|
|
|
{
|
|
|
|
|
if (input == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
SecureString output = new SecureString();
|
|
|
|
|
foreach (char c in input.ToCharArray())
|
|
|
|
|
{
|
|
|
|
|
output.AppendChar(c);
|
|
|
|
|
}
|
|
|
|
|
output.MakeReadOnly();
|
|
|
|
|
return output;
|
|
|
|
|
}
|
2016-02-28 14:21:22 +00:00
|
|
|
|
|
|
|
|
|
public static byte[] SddlToBinary(this string securityDescriptor)
|
|
|
|
|
{
|
|
|
|
|
Validator.AssertNotNullOrWhiteSpace(securityDescriptor, "securityDescriptor");
|
|
|
|
|
byte[] binarySecurityDescriptor;
|
|
|
|
|
Win32ErrorCode result = NativeMethods.ConvertStringSecurityDescriptorToSecurityDescriptor(securityDescriptor, GenericSecurityDescriptor.Revision, out binarySecurityDescriptor);
|
|
|
|
|
Validator.AssertSuccess(result);
|
|
|
|
|
return binarySecurityDescriptor;
|
|
|
|
|
}
|
2015-12-26 22:44:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|