验证网站凭据

时间:2012-08-09 14:21:44

标签: c# validation login

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

要对它进行身份验证,我这样做(工作正常):

NetworkCredential credentials = new NetworkCredential();
            credentials.UserName = AppVars.Username;
            credentials.Password = AppVars.Password;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Credentials = credentials;
            //.....

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

更新代码:

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);
                } 
            }    
        }

域名如下所示:https://xxxxxx-services.zzz999.org/pqg_4/lib/api/sdo/rest/v1

1 个答案:

答案 0 :(得分:1)

使用: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"); 
} 

您可以在此处详细了解:

http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx