How to validate domain credentials?

前端 未结 5 2109
闹比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:22

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Security;
    using System.DirectoryServices.AccountManagement;
    
    public struct Credentials
    {
        public string Username;
        public string Password;
    }
    
    public class Domain_Authentication
    {
        public Credentials Credentials;
        public string Domain;
    
        public Domain_Authentication(string Username, string Password, string SDomain)
        {
            Credentials.Username = Username;
            Credentials.Password = Password;
            Domain = SDomain;
        }
    
        public bool IsValid()
        {
            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, Domain))
            {
                // validate the credentials
                return pc.ValidateCredentials(Credentials.Username, Credentials.Password);
            }
        }
    }
    

提交回复
热议问题