मैं एक एक्सएमएल/सी # एप्लिकेशन बना रहा हूं और मैं इसे एक लॉगिन प्रॉम्प्ट के साथ पॉपअप करना चाहता हूं।विंडोज सुरक्षा कस्टम लॉगिन सत्यापन
मैं जानना चाहता हूं कि CredUIPromptForWindowsCredentials का उपयोग करना संभव है या नहीं।
- दिखाएँ विंडोज सुरक्षा संवाद
- दर्ज की गई उपयोगकर्ता नाम & पासवर्ड प्राप्त
- कस्टम सत्यापन
- प्रदर्शन करना तो सत्यापन उत्तराधिकारी -> जारी रखने के लिए एप्लिकेशन बाकी
- यदि सत्यापन विफल -> -inform अमान्य उपयोगकर्ता नाम के उपयोगकर्ता या पासवर्ड
मैंने पहले से ही Windows Security login form? औरदेखा हैलेकिन वे यह नहीं समझते कि सत्यापन को कैसे संभाला जाए।
मुझे वास्तव में एक छोटा सा उदाहरण पसंद आएगा, जहां उपयोगकर्ता उपयोगकर्ता नाम = "बो" और पासवर्ड = "123" में प्रवेश करता है तो सफलतापूर्वक त्रुटि संदेश प्रदर्शित करता है और उपयोगकर्ता को फिर से प्रयास करने की अनुमति देता है।
ऐप कई कंप्यूटरों पर स्थापित होने जा रहा है।
या यह संभव नहीं है?
अद्यतन
इस सवाल Show Authentication dialog in C# for windows Vista/7
में जवाब से प्रेरित होकर मैं अपेक्षा के अनुरूप काम करने के लिए संशोधित किया है।
कृपया नहीं, कि सत्यापन भाग केवल अवधारणा के सबूत के लिए है।
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);
}
मुझे लगता है कि इसके लिए एक कस्टम फॉर्म बनाना बेहतर है। प्रबंधित करने में आसान और कम जटिल। –
मेरे पास वास्तव में पहले से ही एक कस्टम फॉर्म है, बस यह देखना चाहते हैं कि विंडोज़ का उपयोग करना संभव है या नहीं। इसके अलावा मेरा सबसे अच्छा दिखने वाला नहीं है :-) – gulbaek
और "सर्वश्रेष्ठ दिखने वाले" द्वारा, आपका मतलब यह नहीं है कि यह उपयोगकर्ता को मूर्ख बनाने के लिए पर्याप्त विंडोज सुरक्षा संवाद जैसा नहीं है। – SPE