【发布时间】:2017-03-15 21:56:43
【问题描述】:
此方案适用于需要对其使用 Windows 域身份验证的 UWP 应用。
在创建 Windows 窗体应用程序时,我可以使用下面的代码让用户输入他们的域凭据以验证用户,然后提供在应用程序中执行任务的权限。
下面的代码在 Windows 窗体应用程序中完美运行,就像用户连接到网络一样,它通过服务器进行身份验证,否则使用缓存的凭据进行验证。
如何验证服务器上和 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");
}
}
【问题讨论】:
-
你真的以某种方式解决了这个问题吗?
标签: authentication active-directory uwp credentials