Tôi đang tạo ứng dụng Xaml/C# và tôi muốn ứng dụng bật lên bằng Nhắc đăng nhập.Bảo mật Windows Xác thực đăng nhập tùy chỉnh
Tôi muốn biết nếu có thể sử dụng CredUIPromptForWindowsCredentials.
- Hiện Bảo mật * Windows Dialog
- Lấy tên vào & mật khẩu
- Thực hiện Tuỳ chỉnh xác nhận
- Nếu succes xác nhận -> tiếp tục ứng dụng
- else if xác nhận thất bại -> -inform sử dụng của tên người dùng không hợp lệ hoặc mật khẩu
Tôi đã xem Windows Security login form? và http://www.pinvoke.net/default.aspx/credui/creduipromptforwindowscredentials.html?diff=y nhưng chúng không giải thích cách xử lý xác thực.
Tôi thực sự thích một ví dụ nhỏ, nếu người dùng nhập tên người dùng = "Bo" và mật khẩu = "123" thì thông báo lỗi hiển thị thành công khác và cho phép người dùng thử lại.
Ứng dụng sẽ được cài đặt trên nhiều máy tính.
Hoặc điều này đơn giản là không thể?
Cập nhật
Lấy cảm hứng từ câu trả lời trong câu hỏi này Show Authentication dialog in C# for windows Vista/7
tôi đã sửa đổi mã để làm việc như mong đợi.
Vui lòng không, rằng phần xác thực chỉ dành cho bằng chứng về khái niệm.
WindowsSecurityDialog.cs
public class WindowsSecurityDialog
{
public string CaptionText { get; set; }
public string MessageText { get; set; }
[DllImport("ole32.dll")]
public static extern void CoTaskMemFree(IntPtr ptr);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct CREDUI_INFO
{
public int cbSize;
public IntPtr hwndParent;
public string pszMessageText;
public string pszCaptionText;
public IntPtr hbmBanner;
}
[DllImport("credui.dll", CharSet = CharSet.Auto)]
private static extern bool CredUnPackAuthenticationBuffer(int dwFlags,
IntPtr pAuthBuffer,
uint cbAuthBuffer,
StringBuilder pszUserName,
ref int pcchMaxUserName,
StringBuilder pszDomainName,
ref int pcchMaxDomainame,
StringBuilder pszPassword,
ref int pcchMaxPassword);
[DllImport("credui.dll", CharSet = CharSet.Auto)]
private static extern int CredUIPromptForWindowsCredentials(ref CREDUI_INFO notUsedHere,
int authError,
ref uint authPackage,
IntPtr InAuthBuffer,
uint InAuthBufferSize,
out IntPtr refOutAuthBuffer,
out uint refOutAuthBufferSize,
ref bool fSave,
int flags);
public bool ValidateUser()
{
var credui = new CREDUI_INFO
{
pszCaptionText = CaptionText,
pszMessageText = MessageText
};
credui.cbSize = Marshal.SizeOf(credui);
uint authPackage = 0;
IntPtr outCredBuffer;
uint outCredSize;
bool save = false;
const int loginErrorCode = 1326; //Login Failed
var authError = 0;
while (true)
{
var result = CredUIPromptForWindowsCredentials(ref credui,
authError,
ref authPackage,
IntPtr.Zero,
0,
out outCredBuffer,
out outCredSize,
ref save,
1 /* Generic */);
var usernameBuf = new StringBuilder(100);
var passwordBuf = new StringBuilder(100);
var domainBuf = new StringBuilder(100);
var maxUserName = 100;
var maxDomain = 100;
var maxPassword = 100;
if (result == 0)
{
if (CredUnPackAuthenticationBuffer(0, outCredBuffer, outCredSize, usernameBuf, ref maxUserName,
domainBuf, ref maxDomain, passwordBuf, ref maxPassword))
{
//TODO: ms documentation says we should call this but i can't get it to work
//SecureZeroMem(outCredBuffer, outCredSize);
//clear the memory allocated by CredUIPromptForWindowsCredentials
CoTaskMemFree(outCredBuffer);
var networkCredential = new NetworkCredential()
{
UserName = usernameBuf.ToString(),
Password = passwordBuf.ToString(),
Domain = domainBuf.ToString()
};
//Dummy Code replace with true User Validation
if (networkCredential.UserName == "Bo" && networkCredential.Password == "1234")
return true;
else //login failed show dialog again with login error
{
authError = loginErrorCode;
}
}
}
else return false;
}
}
}
App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
var windowsSecurityDialog = new WindowsSecurityDialog
{
CaptionText = "Enter your credentials",
MessageText = "These credentials will be used to connect to YOUR APP NAME";
};
if (windowsSecurityDialog.ValidateUser())
base.OnStartup(e);
}
Tôi nghĩ tốt hơn là tạo biểu mẫu tùy chỉnh cho việc này. Dễ quản lý và ít phức tạp hơn. –
Tôi thực sự đã có một hình thức tùy chỉnh, chỉ muốn xem nếu nó có thể sử dụng cửa sổ riêng. Ngoài ra tôi không phải là tìm kiếm tốt nhất :-) – gulbaek
Và bởi "không phải là tìm kiếm tốt nhất", bạn có nghĩa là nó không giống với hộp thoại bảo mật cửa sổ đủ để đánh lừa người dùng. – SPE