Chứng thực đăng nhập tài khoản username và password window

0 thích 0 không thích
1 lượt xem
đã hỏi 15 Tháng 3, 2022 trong Lập trình C# bởi nguyenthao (9,000 điểm)
sửa nội dung 15 Tháng 3, 2022 bởi nguyenthao
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);
        
    }

    

2 Câu trả lời

0 thích 0 không thích
đã trả lời 15 Tháng 3, 2022 bởi nguyenthao (9,000 điểm)
Dùng để kiểm tra chứng thực login tài khoản Domain
0 thích 0 không thích
đã trả lời 16 Tháng 3, 2022 bởi nguyenthao (9,000 điểm)
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "laptrinhvb"))
            {
                using (var searcher = new PrincipalSearcher(new UserPrincipal(pc)))
                {                   
                    using (StreamWriter outputFile = new StreamWriter( "D:\\WriteLines.txt"))
                    {
                        int i = 0;
                        foreach (var result in searcher.FindAll())
                        {
                            i++;
                            DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
                            outputFile.WriteLine(i + "> ==================================================================");
                            outputFile.WriteLine("First Name: " + de.Properties["givenName"].Value);
                            outputFile.WriteLine("Last Name : " + de.Properties["sn"].Value);
                            outputFile.WriteLine("SAM account name   : " + de.Properties["samAccountName"].Value);
                            outputFile.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value);
                            outputFile.WriteLine("===================================================================");

                        }
                    }
                }
            }

Get danh sách tài khoản domain.

...