public static Tuple< bool, string> CheckLoginWithDomain(string userName, string password, string domainName = "laptrinhvb")
{
bool isLoginSuccess = false;
string userDisplayName = "";
try
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName))
{
isLoginSuccess = pc.ValidateCredentials(userName, password,
ContextOptions.Negotiate | ContextOptions.SecureSocketLayer |
ContextOptions.SimpleBind | ContextOptions.ServerBind);
var usr = UserPrincipal.FindByIdentity(pc, userName);
if (usr != null)
userDisplayName = usr.DisplayName;
}
}
catch(Exception ex)
{
isLoginSuccess = false;
userDisplayName = ex.Message;
}
return Tuple.Create(isLoginSuccess, userDisplayName);
}
public static Tuple<bool, string> ChangePasswordDomain(string userName, string oldPassword, string newPassword)
{
bool isSuccess = false;
string message = "";
try
{
using (var context = new PrincipalContext(ContextType.Domain))
{
using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName))
{
user.ChangePassword(oldPassword, newPassword);
user.Save();
isSuccess = true;
message = "Change password " + userName + " successful.";
}
}
}
catch (Exception ex)
{
isSuccess = false;
message = ex.Message;
}
return Tuple.Create(isSuccess, message);
}