C# 验证网站凭据

C# 验证网站凭据,c#,validation,login,C#,Validation,Login,我有一个网站:https://blahblah.com" 为了对其进行身份验证,我执行以下操作(这很好): 但是,如果我想添加登录功能,我该如何验证用户名和密码呢 更新代码: private void btnLogIn_Click(object sender, EventArgs e) { Properties.Settings.Default.Username = txtUserName.Text; Properties.Sett

我有一个网站:https://blahblah.com"

为了对其进行身份验证,我执行以下操作(这很好):

但是,如果我想添加登录功能,我该如何验证用户名和密码呢

更新代码:

private void btnLogIn_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Username = txtUserName.Text;
            Properties.Settings.Default.Password = txtPassword.Text;

            using (PrincipalContext pc = new PrincipalContext( ContextType.Domain,  AppVars.ixLibraryConnectionTestURL))
            {
                try
                {
                    bool isValid = false;
                    isValid = pc.ValidateCredentials(AppVars.Username, AppVars.Password);
                    if (isValid == true)
                    {
                        //just testing
                        MessageBox.Show("is valid");
                    }
                    else
                    {
                        //just testing
                        MessageBox.Show("is not valid");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                } 
            }    
        }

域名如下所示:

使用:System.DirectoryServices.AccountManagement命名空间

// create a "principal context" - e.g. your domain (could be machine, too) 

using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN")) 
{     

// validate the credentials    
 bool isValid = pc.ValidateCredentials("myuser", "mypassword"); 
} 
您可以在此处阅读更多信息:


正确。我在.NET3.5中使用C#。为什么我不能添加这个名称空间?顺便说一句,我使用.NET3.5。当我使用System.Direc键入时(它找不到directoryServices)…没关系,我添加了引用。让met立即尝试此代码。PrincipalServerDownException未经处理。无法联系服务器。请尝试在用户名前加上域名。。示例:pc.ValidateCredentials(“域\\用户名”,“密码”);链接也不起作用。链接不起作用,因为它是一个示例lol。。我要试着给域名加前缀
// create a "principal context" - e.g. your domain (could be machine, too) 

using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN")) 
{     

// validate the credentials    
 bool isValid = pc.ValidateCredentials("myuser", "mypassword"); 
}