Repository navigation
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
131 lines (112 loc) · 4.65 KB
/
Copy pathNativeMethods.cs
File metadata and controls
131 lines (112 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
#pragma warning disable 169
// ReSharper disable once CheckNamespace
namespace HGM.Hotbird64.LicenseManager
{
public static partial class NativeMethods
{
public class AuthPrompt : IDisposable
{
private struct CREDUI_INFO
{
public int cbSize;
private readonly IntPtr hwndParent;
[MarshalAs(UnmanagedType.LPWStr)]
public string MessageText;
[MarshalAs(UnmanagedType.LPWStr)]
public string CaptionText;
private readonly IntPtr hbmBanner;
}
[DllImport("credui", CharSet = CharSet.Unicode)]
private static extern CredUiReturnCodes CredUIPromptForCredentials(
ref CREDUI_INFO creditUr,
string targetName,
IntPtr reserved1,
int iError,
StringBuilder userName,
int maxUserName,
StringBuilder password,
int maxPassword,
[MarshalAs(UnmanagedType.Bool)] ref bool pfSave,
CREDUI_FLAGS flags);
[DllImport("credui", CharSet = CharSet.Unicode)]
private static extern CredUiReturnCodes CredUIConfirmCredentials(string targetName, [In, MarshalAs(UnmanagedType.Bool)] bool bConfirm);
[Flags]
private enum CREDUI_FLAGS
{
INCORRECT_PASSWORD = 0x1,
DO_NOT_PERSIST = 0x2,
REQUEST_ADMINISTRATOR = 0x4,
EXCLUDE_CERTIFICATES = 0x8,
REQUIRE_CERTIFICATE = 0x10,
SHOW_SAVE_CHECK_BOX = 0x40,
ALWAYS_SHOW_UI = 0x80,
REQUIRE_SMARTCARD = 0x100,
PASSWORD_ONLY_OK = 0x200,
VALIDATE_USERNAME = 0x400,
COMPLETE_USERNAME = 0x800,
PERSIST = 0x1000,
SERVER_CREDENTIAL = 0x4000,
EXPECT_CONFIRMATION = 0x20000,
GENERIC_CREDENTIALS = 0x40000,
USERNAME_TARGET_CREDENTIALS = 0x80000,
KEEP_USERNAME = 0x100000,
}
public enum CredUiReturnCodes
{
NO_ERROR = 0,
ERROR_CANCELLED = 1223,
ERROR_NO_SUCH_LOGON_SESSION = 1312,
ERROR_NOT_FOUND = 1168,
ERROR_INVALID_ACCOUNT_NAME = 1315,
ERROR_INSUFFICIENT_BUFFER = 122,
ERROR_INVALID_PARAMETER = 87,
ERROR_INVALID_FLAGS = 1004,
}
public CredUiReturnCodes PromptForPassword(bool showUI, string messageText, string captionText)
{
StringBuilder userPassword = new(256), userID = new(256);
CREDUI_INFO credUI = new();
credUI.cbSize = Marshal.SizeOf(credUI);
credUI.CaptionText = captionText;
credUI.MessageText = messageText;
bool save = true;
CREDUI_FLAGS flags = (showUI ? CREDUI_FLAGS.ALWAYS_SHOW_UI : 0) | CREDUI_FLAGS.GENERIC_CREDENTIALS | CREDUI_FLAGS.SHOW_SAVE_CHECK_BOX | CREDUI_FLAGS.EXPECT_CONFIRMATION;
CredUiReturnCodes returnCode = CredUIPromptForCredentials(ref credUI, ServerName, IntPtr.Zero, 0, userID, 100, userPassword, 100, ref save, flags);
User = userID.ToString();
SecurePassword = new SecureString();
for (int i = 0; i < userPassword.Length; i++)
{
SecurePassword.AppendChar(userPassword[i]);
}
return returnCode;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (SecurePassword != null)
{
if (disposing)
{
SecurePassword.Dispose();
}
SecurePassword = null;
}
}
public CredUiReturnCodes ConfirmCredentials(bool correct)
{
return CredUIConfirmCredentials(ServerName, correct);
}
public string User { get; private set; }
public SecureString SecurePassword { get; private set; }
public string ServerName;
}
}
}