如何在 UWP 应用中验证 Windows 凭据 [英] How to validate Windows Credentials in a UWP app

查看:29
本文介绍了如何在 UWP 应用中验证 Windows 凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此方案适用于需要使用 Windows 域身份验证的 UWP 应用.

This scenario is for a UWP app for which Windows domain authentication needs to be used.

在创建 Windows 窗体应用程序时,我可以使用下面的代码让用户输入他们的域凭据来验证用户,然后提供在应用程序中执行任务的权限.

When creating Windows Forms applications, I can use the code below to have the user input their domain credentials to verify the user and then provide permission to perform tasks within the application.

下面的代码在 Windows 窗体应用程序中完美运行,就像用户连接到网络一样,它通过服务器进行身份验证,否则使用缓存的凭据进行验证.

The code below works perfectly in Windows Forms applications as in if the user is connected to the network, it authenticates with the server and otherwise validates with the cached credentials.

如何验证服务器上和本地缓存在 UWP 应用中的 Active Directory 凭据?

    private void button1_Click(object sender, EventArgs e)
    {
        bool valid = false;
        try
        {
            using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
            {
                valid = context.ValidateCredentials(textBox1.Text, textBox2.Text);
                if (valid)
                {
                    // Login with server credentials successful
                    MessageBox.Show("Successfully Logged In");
                }
                else
                {
                    // Login with server credentials failed
                    MessageBox.Show("Invalid UserName/Password");
                }
            }
        }
        catch (PrincipalServerDownException exPSD)
        {
            //server is down; check local cache
            MessageBox.Show("server is down; check local cache");
            valid = false;
            using (PrincipalContext checkpass = new PrincipalContext(ContextType.Machine)) //checks local machine first
            {


                valid = checkpass.ValidateCredentials(textBox1.Text, textBox2.Text);


                if (valid)
                {
                    // Login with cached credentials successful  
                    MessageBox.Show("Successfully Logged In");
                }
                else
                {
                    // Login with cached credentials failed
                    MessageBox.Show("Invalid UserName/Password");
                }

            }
        }
        catch (Exception ex)
        {
            //some other exception; show general message
            MessageBox.Show("some other exception; show general message");
        }
    }

推荐答案

Web 帐户管理示例 展示了如何根据 AD 验证凭据.

The Web Account Management sample shows how to validate credentials against AD.

这篇关于如何在 UWP 应用中验证 Windows 凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆