Register the user from the login form itself I want to create new accounts only if user name and password have matched with that of with Active Directory.

So if a completely new user tries to login and enters correct Active Directory login &amp; password then I'm adding him/her using user_save in the form validator.

But at the same time I'm returning with error that "please re-login".

However I want a seamless login. That is if a new user tries to login -- I'll create a new user and at the same time he/she should also be logged in.

How can I do this?
I had a similar use case in the past and i am giving some snippet, hope will help you. The key was user_login_finalize and then redirecting internally to the same page.

Load the user if already present or create and then load him

$user = user_load_by_name_and_password($username, $password);
if ($user == FALSE) {
  $newuser = array(
    'name' =&gt; $username,
    'pass' =&gt; $password, // some password
    'mail' =&gt; $email, // some email
    'status' =&gt; 1,
    'init' =&gt; $email, // some email id
    'roles' =&gt; $roles, // some roles
  );
  // New user is saved and loaded
  $user = user_save(drupal_anonymous_user(), $newuser);
}
user_login_finalize();
drupal_goto(get_current_url_with_query_params());


http://api.drupal.org/api/drupal/modules!user!user.module/function/user_login_finalize/7
Finalize the login process. Must be called when logging in a user

drupal_goto must be called as user_login_finalize() bootstraps the currently logged in user too late and hence will throw 'access denied' even for the page for which the user has permission. drupal_goto() makes sure that the user is properly bootstrapped and then is made to access the page again.

It is equivalent to reloading the page after the user has logged in but this is the only way i got my external login work. Refer User login using a token or ID (no password)