Skip to content

Instantly share code, notes, and snippets.

@yareally
Last active September 14, 2015 21:04
Show Gist options
  • Save yareally/533fc167debc6fe31775 to your computer and use it in GitHub Desktop.
Save yareally/533fc167debc6fe31775 to your computer and use it in GitHub Desktop.
namespace ServicesTool2.ViewModel
{
using System;
using System.DirectoryServices.AccountManagement;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
public class ServiceAccountViewModel : ViewModelBase
{
public ServiceAccountViewModel()
{
Domain = Environment.UserDomainName;
}
public string Domain { get; set; }
public string Username { get; set; }
public ICommand VerifyCommand => new RelayCommand<object>(Verify);
private async Task Verify(object parameter)
{
var passwordBox = parameter as PasswordBox;
if (passwordBox != null) {
string password = passwordBox.Password;
try {
using (var pc = new PrincipalContext(ContextType.Domain, Domain)) {
bool isValid = await Task<bool>.Factory.StartNew(() => pc.ValidateCredentials(Username, password));
MessageBox.Show(isValid ? "success" : "failure");
}
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment