加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Windows安全自定义登录验证

发布时间:2020-12-15 07:41:59 所属栏目:百科 来源:网络整理
导读:我正在创建一个Xaml / C#应用程序,我希望它能够通过登录提示弹出. 我想知道是否可以使用CredUIPromptForWindowsCredentials. 显示Windows安全对话框 获取输入的用户名密码 执行自定义验证 如果验证成功 – 继续app 如果验证失败,则为 – – 告知用户无效的用
我正在创建一个Xaml / C#应用程序,我希望它能够通过登录提示弹出.

我想知道是否可以使用CredUIPromptForWindowsCredentials.

>显示Windows安全对话框
>获取输入的用户名&密码
>执行自定义验证
>如果验证成功 – >继续app
>如果验证失败,则为> – > – 告知用户无效的用户名或密码

我已经看过Windows Security login form?和http://www.pinvoke.net/default.aspx/credui/creduipromptforwindowscredentials.html?diff=y,但他们没有解释如何处理验证.

我真的很喜欢一个小例子,如果用户输入username =“Bo”和password =“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,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);
        }

解决方法

您将在 Ookii dialogs使用CredUIPromptForWindowsCredentials找到WPF和WinForms的完整实现.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读