2012-04-20 21 views
7

मैं एक एक्सएमएल/सी # एप्लिकेशन बना रहा हूं और मैं इसे एक लॉगिन प्रॉम्प्ट के साथ पॉपअप करना चाहता हूं।विंडोज सुरक्षा कस्टम लॉगिन सत्यापन

मैं जानना चाहता हूं कि 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); 
     } 
+0

मुझे लगता है कि इसके लिए एक कस्टम फॉर्म बनाना बेहतर है। प्रबंधित करने में आसान और कम जटिल। –

+1

मेरे पास वास्तव में पहले से ही एक कस्टम फॉर्म है, बस यह देखना चाहते हैं कि विंडोज़ का उपयोग करना संभव है या नहीं। इसके अलावा मेरा सबसे अच्छा दिखने वाला नहीं है :-) – gulbaek

+0

और "सर्वश्रेष्ठ दिखने वाले" द्वारा, आपका मतलब यह नहीं है कि यह उपयोगकर्ता को मूर्ख बनाने के लिए पर्याप्त विंडोज सुरक्षा संवाद जैसा नहीं है। – SPE

उत्तर

4

आपको Ookii dialogs पर CredUIPromptForWindowsCredentials का उपयोग करके WPF और WinForms के लिए एक पूर्ण कार्यान्वयन मिलेगा।

3

मैं जब मैं सोच यह संभव हो सकता है शुरू कर दिया एक छोटे से भयभीत था।

उत्तर हाँ और नहीं है। आप नेटवर्क डोमेन और उपयोगकर्ता नाम धारण कर सकते हैं, लेकिन (भलाई का शुक्र है), आप वास्तविक पासवर्ड को पकड़ नहीं सकते हैं, केवल पासवर्ड का हैश।

PInvoke से भारी उधार लेना, यहां एक नमूना WPF ऐप है जो उपयोगकर्ता नाम और पासवर्ड लाता है और आउटपुट करता है।

कोड

using System; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Windows; 
using System.Windows.Interop; 

namespace LoginDialog 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      // Declare/initialize variables. 
      bool save = false; 
      int errorcode = 0; 
      uint dialogReturn; 
      uint authPackage = 0; 
      IntPtr outCredBuffer; 
      uint outCredSize; 

      // Create the CREDUI_INFO struct. 
      CREDUI_INFO credui = new CREDUI_INFO(); 
      credui.cbSize = Marshal.SizeOf(credui); 
      credui.pszCaptionText = "Connect to your application"; 
      credui.pszMessageText = "Enter your credentials!"; 
      credui.hwndParent = new WindowInteropHelper(this).Handle; 

      // Show the dialog. 
      dialogReturn = CredUIPromptForWindowsCredentials(
       ref credui, 
       errorcode, 
       ref authPackage, 
       (IntPtr)0, // You can force that a specific username is shown in the dialog. Create it with 'CredPackAuthenticationBuffer()'. Then, the buffer goes here... 
       0,   // ...and the size goes here. You also have to add CREDUIWIN_IN_CRED_ONLY to the flags (last argument). 
       out outCredBuffer, 
       out outCredSize, 
       ref save, 
       0); // Use the PromptForWindowsCredentialsFlags Enum here. You can use multiple flags if you seperate them with | . 

      if (dialogReturn == 1223) // Result of 1223 means the user canceled. Not sure if other errors are ever returned. 
       textBox1.Text += ("User cancelled!"); 
      if (dialogReturn != 0) // Result of something other than 0 means...something, I'm sure. Either way, failed or canceled. 
       return; 

      var domain = new StringBuilder(100); 
      var username = new StringBuilder(100); 
      var password = new StringBuilder(100); 
      int maxLength = 100; // Note that you can have different max lengths for each variable if you want. 

      // Unpack the info from the buffer. 
      CredUnPackAuthenticationBuffer(0, outCredBuffer, outCredSize, username, ref maxLength, domain, ref maxLength, password, ref maxLength); 

      // Clear the memory allocated by CredUIPromptForWindowsCredentials. 
      CoTaskMemFree(outCredBuffer); 

      // Output info, escaping whitespace characters for the password. 
      textBox1.Text += String.Format("Domain: {0}\n", domain); 
      textBox1.Text += String.Format("Username: {0}\n", username); 
      textBox1.Text += String.Format("Password (hashed): {0}\n", EscapeString(password.ToString())); 
     } 

     public static string EscapeString(string s) 
     { 
      // Formatted like this only for you, SO. 
      return s 
       .Replace("\a", "\\a") 
       .Replace("\b", "\\b") 
       .Replace("\f", "\\f") 
       .Replace("\n", "\\n") 
       .Replace("\r", "\\r") 
       .Replace("\t", "\\t") 
       .Replace("\v", "\\v"); 
     } 

     #region DLLImports 
     [DllImport("ole32.dll")] 
     public static extern void CoTaskMemFree(IntPtr ptr); 

     [DllImport("credui.dll", CharSet = CharSet.Unicode)] 
     private static extern uint CredUIPromptForWindowsCredentials(ref CREDUI_INFO notUsedHere, int authError, ref uint authPackage, IntPtr InAuthBuffer, 
      uint InAuthBufferSize, out IntPtr refOutAuthBuffer, out uint refOutAuthBufferSize, ref bool fSave, PromptForWindowsCredentialsFlags flags); 

     [DllImport("credui.dll", CharSet = CharSet.Unicode)] 
     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); 
     #endregion 

     #region Structs and Enums 
     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
     private struct CREDUI_INFO 
     { 
      public int cbSize; 
      public IntPtr hwndParent; 
      public string pszMessageText; 
      public string pszCaptionText; 
      public IntPtr hbmBanner; 
     } 

     private enum PromptForWindowsCredentialsFlags 
     { 
      /// <summary> 
      /// The caller is requesting that the credential provider return the user name and password in plain text. 
      /// This value cannot be combined with SECURE_PROMPT. 
      /// </summary> 
      CREDUIWIN_GENERIC = 0x1, 
      /// <summary> 
      /// The Save check box is displayed in the dialog box. 
      /// </summary> 
      CREDUIWIN_CHECKBOX = 0x2, 
      /// <summary> 
      /// Only credential providers that support the authentication package specified by the authPackage parameter should be enumerated. 
      /// This value cannot be combined with CREDUIWIN_IN_CRED_ONLY. 
      /// </summary> 
      CREDUIWIN_AUTHPACKAGE_ONLY = 0x10, 
      /// <summary> 
      /// Only the credentials specified by the InAuthBuffer parameter for the authentication package specified by the authPackage parameter should be enumerated. 
      /// If this flag is set, and the InAuthBuffer parameter is NULL, the function fails. 
      /// This value cannot be combined with CREDUIWIN_AUTHPACKAGE_ONLY. 
      /// </summary> 
      CREDUIWIN_IN_CRED_ONLY = 0x20, 
      /// <summary> 
      /// Credential providers should enumerate only administrators. This value is intended for User Account Control (UAC) purposes only. We recommend that external callers not set this flag. 
      /// </summary> 
      CREDUIWIN_ENUMERATE_ADMINS = 0x100, 
      /// <summary> 
      /// Only the incoming credentials for the authentication package specified by the authPackage parameter should be enumerated. 
      /// </summary> 
      CREDUIWIN_ENUMERATE_CURRENT_USER = 0x200, 
      /// <summary> 
      /// The credential dialog box should be displayed on the secure desktop. This value cannot be combined with CREDUIWIN_GENERIC. 
      /// Windows Vista: This value is not supported until Windows Vista with SP1. 
      /// </summary> 
      CREDUIWIN_SECURE_PROMPT = 0x1000, 
      /// <summary> 
      /// The credential provider should align the credential BLOB pointed to by the refOutAuthBuffer parameter to a 32-bit boundary, even if the provider is running on a 64-bit system. 
      /// </summary> 
      CREDUIWIN_PACK_32_WOW = 0x10000000, 
     } 
     #endregion 
    } 
} 

टेस्ट

  1. एक नया WPF आवेदन LoginDialog बुलाया बनाएँ।
  2. TextBoxMainWindow.xaml फ़ाइल textBox1 नामित फ़ाइल में ड्रॉप करें।
  3. कोड को MainWindow.xaml.cs फ़ाइल में बदलें।
  4. भागो!

नमूना आउटपुट

पासवर्ड "पासवर्ड" को देखते हुए यहां उत्पादन होता है।

Domain: 
Username: EXAMPLE\fake 
Password (hashed): @@D\a\b\f\n\rgAAAAAU-JPAAAAAAweFpM4nPlOUfKi83JLsl4jjh6nMX34yiH 

टिप्पणियाँ

इस WPF के लिए काम करता है। यह right permissions के साथ सिल्वरलाइट के लिए काम कर सकता है।

मुझे नहीं पता कि कोई भी वैध कस्टम सत्यापन के लिए ऐसा क्यों करेगा। यदि आप अपने ऐप के लिए लॉगिन बनाना चाहते हैं, तो मैं क्लाइंट को एसएसएल (https: //) के माध्यम से एएसपी.NET पृष्ठ या वेब सेवा से कनेक्ट करने का सुझाव दूंगा जो LINQ से SQL का उपयोग करके प्रदान किए गए प्रमाण-पत्रों की जांच करेगा। इसके बाद ग्राहक को पास/असफल प्रतिक्रिया भेज सकती है।

ओह, और भगवान के प्यार और पवित्र सभी के लिए, salt and hash your users' passwords

नोट: यदि आप इस लॉगिन का उपयोग करना चाहते हैं ताकि उपयोगकर्ता को बिना किसी खाते/भुगतान के आपके ऐप का उपयोग करने से रोका जा सके, सभी उपरोक्त स्टैंड हैं, लेकिन लोगों को रिवर्स इंजीनियरिंग से रोकने और ऐप को क्रैक करने के लिए पर्याप्त नहीं होगा (उदाहरण के लिए, यह सोचने में यह धोखा दे रहा है कि यह पास संदेश प्राप्त हुआ है)। उस प्रकार का डीआरएम एक संपूर्ण 'नोटर बॉलगेम है।

+0

एक संभावित समाधान के साथ मेरा प्रश्न अपडेट किया गया। सुरक्षा के बारे में आपकी जानकारी साझा करने के लिए धन्यवाद, लेकिन यह प्रश्न विंडोज सुरक्षा संवाद फ़ॉर्म का उपयोग करने के बारे में अधिक है। मैं पहले से ही अन्य सावधानी के बीच एक सुरक्षित उपयोगकर्ता सत्यापन का उपयोग कर रहा हूँ। – gulbaek

+1

अनपॅक 'CredUnPackAuthenticationBuffer (1, ...' के दौरान आप इसे डिक्रिप्ट करके अन-पासवर्ड प्राप्त कर सकते हैं – HodlDwon

संबंधित मुद्दे