diff --git a/modules/openid/openid.pages.inc b/modules/openid/openid.pages.inc
index 8a52f20..d7fc6b2 100644
--- a/modules/openid/openid.pages.inc
+++ b/modules/openid/openid.pages.inc
@@ -42,6 +42,28 @@ function openid_user_identities($account) {
       ))
       ->execute();
     drupal_set_message(t('Successfully added %identity', array('%identity' => $identity)));
+    $saved_identity = db_query("SELECT * FROM {authmap} WHERE authname=:authname", array(':authname' => $result['openid.claimed_id']))->fetchObject();
+    // Prevent DB integrity errors. We need to check the uniqueness of the
+    // claimed identifier here because claimed id present in response could be
+    // different than the claimed id which entered the user. E.g. it could
+    // have attached fragment to allow provider to recycle the identifiers.
+    if ($saved_identity && $saved_identity->uid == $account->uid) {
+      drupal_set_message(t('That OpenID %identity is already associated with this account.', array('%identity' => $saved_identity->authname)), 'error');
+    }
+    elseif ($saved_identity) {
+      drupal_set_message(t('That OpenID is already in use on this site.'), 'error');
+    }
+    else {
+      $identity = $result['openid.claimed_id'];
+      $query = db_insert('authmap')
+        ->fields(array(
+          'uid' => $account->uid,
+          'authname' => $identity,
+          'module' => 'openid',
+        ))
+        ->execute();
+      drupal_set_message(t('Successfully added %identity', array('%identity' => $identity)));
+    }
   }
 
   $header = array(t('OpenID'), t('Operations'));
