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

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

问题描述

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

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

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

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 Forms应用程序中可以完美运行,就像用户连接到网络,通过服务器进行身份验证或通过缓存的凭据进行验证一样.

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应用中本地缓存的活动目录凭据上进行验证?

    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");
        }
    }

推荐答案

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

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

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