2

我有一个网站:“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

4

1 回答 1

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

于 2012-08-09T14:32:26.063 回答