How to validate domain credentials?

前端 未结 5 2097
闹比i
闹比i 2020-11-27 10:04

I want to validate a set of credentials against the domain controller. e.g.:

Username: STACKOVERFLOW\\joel
Password: splotchy

Method 1. Qu

5条回答
  •  粉色の甜心
    2020-11-27 10:43

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.DirectoryServices.AccountManagement;
    
    class WindowsCred
    {
        private const string SPLIT_1 = "\\";
    
        public static bool ValidateW(string UserName, string Password)
        {
            bool valid = false;
            string Domain = "";
    
            if (UserName.IndexOf("\\") != -1)
            {
                string[] arrT = UserName.Split(SPLIT_1[0]);
                Domain = arrT[0];
                UserName = arrT[1];
            }
    
            if (Domain.Length == 0)
            {
                Domain = System.Environment.MachineName;
            }
    
            using (PrincipalContext context = new PrincipalContext(ContextType.Domain, Domain)) 
            {
                valid = context.ValidateCredentials(UserName, Password);
            }
    
            return valid;
        }
    }
    

    Kashif Mushtaq Ottawa, Canada

提交回复
热议问题