diff --git a/app/models/user.rb b/app/models/user.rb
index 8a69fda00b444182d0d7203a8f0f24ae8ff300a0..496ca2137047b9acf9fdb346041cd2db764f92db 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -111,7 +111,8 @@ class User < Principal
       # user is not yet registered, try to authenticate with available sources
       attrs = AuthSource.authenticate(login, password)
       if attrs
-        user = new(*attrs)
+        attributes = *attrs
+        user = new(attributes.symbolize_keys.except(:dn))
         user.login = login
         user.language = Setting.default_language
         if user.save
diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb
index a94870dbc6bb7a791ecc6ba65eefc4555b73111d..8d5ce974210017de53f986519e90347f202c7337 100644
--- a/test/unit/user_test.rb
+++ b/test/unit/user_test.rb
@@ -120,6 +120,36 @@ class UserTest < ActiveSupport::TestCase
     assert_equal nil, user  
   end
   
+  if ldap_configured?
+    context "#try_to_login using LDAP" do
+      context "on the fly registration" do
+        setup do
+          @auth_source = AuthSourceLdap.generate!(:name => 'localhost',
+                                                  :host => '127.0.0.1',
+                                                  :port => 389,
+                                                  :base_dn => 'OU=Person,DC=redmine,DC=org',
+                                                  :attr_login => 'uid',
+                                                  :attr_firstname => 'givenName',
+                                                  :attr_lastname => 'sn',
+                                                  :attr_mail => 'mail',
+                                                  :onthefly_register => true)
+
+        end
+
+        context "with a successful authentication" do
+          should "create a new user account" do
+            assert_difference('User.count') do
+              User.try_to_login('edavis', '123456')
+            end
+          end
+        end
+      end
+    end
+
+  else
+    puts "Skipping LDAP tests."
+  end
+  
   def test_create_anonymous
     AnonymousUser.delete_all
     anon = User.anonymous