Index: src/main/java/com/xpn/xwiki/user/impl/xwiki/XWikiAuthServiceImpl.java
===================================================================
--- src/main/java/com/xpn/xwiki/user/impl/xwiki/XWikiAuthServiceImpl.java	(revision 24987)
+++ src/main/java/com/xpn/xwiki/user/impl/xwiki/XWikiAuthServiceImpl.java	(working copy)
@@ -39,6 +39,9 @@
 import org.securityfilter.config.SecurityConfig;
 import org.securityfilter.filter.SecurityRequestWrapper;
 import org.securityfilter.realm.SimplePrincipal;
+import org.xwiki.bridge.DocumentName;
+import org.xwiki.bridge.DocumentNameFactory;
+import org.xwiki.bridge.DocumentNameSerializer;
 
 import com.xpn.xwiki.XWiki;
 import com.xpn.xwiki.XWikiContext;
@@ -49,6 +52,7 @@
 import com.xpn.xwiki.user.api.XWikiAuthService;
 import com.xpn.xwiki.user.api.XWikiUser;
 import com.xpn.xwiki.util.Util;
+import com.xpn.xwiki.web.Utils;
 
 /**
  * Default implementation of {@link XWikiAuthService}.
@@ -60,6 +64,17 @@
     private static final Log LOG = LogFactory.getLog(XWikiAuthServiceImpl.class);
 
     /**
+     * Used to convert a string into a proper Document Name.
+     */
+    private DocumentNameFactory documentNameFactory = Utils.getComponent(DocumentNameFactory.class);
+
+    /**
+     * Used to convert a proper Document Name to string.
+     */
+    private DocumentNameSerializer compactDocumentNameSerializer =
+            Utils.getComponent(DocumentNameSerializer.class, "compact");
+
+    /**
      * Each wiki has its own authenticator.
      */
     protected Map<String, XWikiAuthenticator> authenticators = new ConcurrentHashMap<String, XWikiAuthenticator>();
@@ -221,15 +236,16 @@
             }
 
             // TODO : This code need to be moved elsewhere (appropriately).
-            Principal user = wrappedRequest.getUserPrincipal();
-            if (context.getAction().equals("logout") && user != null) {
+            String userName = getContextUserName(wrappedRequest.getUserPrincipal(), context);
+
+            if (context.getAction().equals("logout") && userName != null) {
                 // Release all active locks on current wiki.
                 try {
                     xwiki.getHibernateStore().beginTransaction(context);
                     Session session = xwiki.getHibernateStore().getSession(context);
                     String sql = "delete from XWikiLock as lock where lock.userName=:userName";
                     Query query = session.createQuery(sql);
-                    query.setString("userName", user.getName());
+                    query.setString("userName", userName);
                     query.executeUpdate();
                 } catch (Exception e) {
                     throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
@@ -245,7 +261,7 @@
                 // If we're in a non-main wiki & the user is global,
                 // switch to the global wiki and delete locks held there.
                 if (xwiki.isVirtualMode() && !context.getMainXWiki().equals(context.getDatabase())
-                    && user.getName().startsWith(context.getMainXWiki() + ":")) {
+                    && userName.startsWith(context.getMainXWiki() + ":")) {
                     String cdb = context.getDatabase();
                     // switch to main wiki.
                     context.setDatabase(context.getMainXWiki());
@@ -254,7 +270,7 @@
                         Session session = xwiki.getHibernateStore().getSession(context);
                         String sql = "delete from XWikiLock as lock where lock.userName=:userName";
                         Query query = session.createQuery(sql);
-                        String localName = user.getName().substring(user.getName().indexOf(":") + 1);
+                        String localName = userName.substring(userName.indexOf(":") + 1);
                         query.setString("userName", localName);
                         query.executeUpdate();
                     } catch (Exception e) {
@@ -283,16 +299,16 @@
             }
 
             if (LOG.isInfoEnabled()) {
-                if (user != null) {
-                    LOG.info("User " + user.getName() + " is authentified");
+                if (userName != null) {
+                    LOG.info("User " + userName + " is authentified");
                 }
             }
 
-            if (user == null) {
+            if (userName == null) {
                 return null;
             }
 
-            return new XWikiUser(user.getName());
+            return new XWikiUser(userName);
         } catch (Exception e) {
             LOG.error("Failed to authenticate", e);
 
@@ -330,18 +346,18 @@
                 return null;
             }
 
-            Principal user = wrappedRequest.getUserPrincipal();
+            Principal principal = wrappedRequest.getUserPrincipal();
             if (LOG.isInfoEnabled()) {
-                if (user != null) {
-                    LOG.info("User " + user.getName() + " is authentified");
+                if (principal != null) {
+                    LOG.info("User " + principal.getName() + " is authentified");
                 }
             }
 
-            if (user == null) {
+            if (principal == null) {
                 return null;
             }
 
-            return new XWikiUser(user.getName());
+            return new XWikiUser(getContextUserName(principal, context));
         } catch (Exception e) {
             LOG.error("Failed to authenticate", e);
 
@@ -349,6 +365,20 @@
         }
     }
 
+    private String getContextUserName(Principal principal, XWikiContext context)
+    {
+        String contextUserName;
+
+        if (principal != null) {
+            DocumentName userDocumentName = this.documentNameFactory.createDocumentName(principal.getName());
+            contextUserName = this.compactDocumentNameSerializer.serialize(userDocumentName);
+        } else {
+            contextUserName = null;
+        }
+
+        return contextUserName;
+    }
+
     public void showLogin(XWikiContext context) throws XWikiException
     {
         try {
@@ -515,8 +545,8 @@
             if (doc.getObject("XWiki.XWikiUsers") != null) {
                 String passwd = doc.getStringValue("XWiki.XWikiUsers", "password");
                 password =
-                    ((PasswordClass) context.getWiki().getClass("XWiki.XWikiUsers", context).getField("password")).getEquivalentPassword(
-                        passwd, password);
+                        ((PasswordClass) context.getWiki().getClass("XWiki.XWikiUsers", context).getField("password")).getEquivalentPassword(
+                            passwd, password);
 
                 result = (password.equals(passwd));
             }
Index: src/main/java/com/xpn/xwiki/user/impl/xwiki/MyFormAuthenticator.java
===================================================================
--- src/main/java/com/xpn/xwiki/user/impl/xwiki/MyFormAuthenticator.java	(revision 24987)
+++ src/main/java/com/xpn/xwiki/user/impl/xwiki/MyFormAuthenticator.java	(working copy)
@@ -34,6 +34,7 @@
 import org.securityfilter.authenticator.FormAuthenticator;
 import org.securityfilter.filter.SecurityRequestWrapper;
 import org.securityfilter.filter.URLPatternMatcher;
+import org.securityfilter.realm.SimplePrincipal;
 
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.XWikiException;
@@ -130,11 +131,14 @@
         // persistent logins are enabled, and the persistent login info is present in this request
         if (this.persistentLoginManager != null) {
             String username =
-                convertUsername(this.persistentLoginManager.getRememberedUsername(request, response), context);
+                    convertUsername(this.persistentLoginManager.getRememberedUsername(request, response), context);
             String password = this.persistentLoginManager.getRememberedPassword(request, response);
 
             Principal principal = request.getUserPrincipal();
 
+            // 1) if user is not already authenticated, authenticate
+            // 2) if authenticated user for this session does not has the same name, authenticate
+            // 3) if xwiki.authentication.always is set to 1 in xwiki.cfg file, authenticate
             if (principal == null || !StringUtils.endsWith(principal.getName(), "XWiki." + username)
                 || context.getWiki().ParamAsLong("xwiki.authentication.always", 0) == 1) {
                 principal = authenticate(username, password, context);
@@ -143,6 +147,12 @@
                     if (LOG.isDebugEnabled()) {
                         LOG.debug("User " + principal.getName() + " has been authentified from cookie");
                     }
+
+                    // make sure the Principal contains wiki name information
+                    if (!StringUtils.contains(principal.getName(), ':')) {
+                        principal = new SimplePrincipal(context.getDatabase() + ":" + principal.getName());
+                    }
+
                     request.setUserPrincipal(principal);
                 } else {
                     // Failed to authenticate, better cleanup the user stored in the session
@@ -202,6 +212,11 @@
                 }
             }
 
+            // make sure the Principal contains wiki name information
+            if (!StringUtils.contains(principal.getName(), ':')) {
+                principal = new SimplePrincipal(context.getDatabase() + ":" + principal.getName());
+            }
+
             request.setUserPrincipal(principal);
             Boolean bAjax = (Boolean) context.get("ajax");
             if ((bAjax == null) || (!bAjax.booleanValue())) {
@@ -227,6 +242,7 @@
             }
             response.setStatus(rCode); // TODO: Does this work? (200 in case of error)
         }
+
         return true;
     }
 
Index: src/main/java/com/xpn/xwiki/user/impl/xwiki/MyBasicAuthenticator.java
===================================================================
--- src/main/java/com/xpn/xwiki/user/impl/xwiki/MyBasicAuthenticator.java	(revision 24987)
+++ src/main/java/com/xpn/xwiki/user/impl/xwiki/MyBasicAuthenticator.java	(working copy)
@@ -28,9 +28,11 @@
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.lang.StringUtils;
 import org.securityfilter.authenticator.BasicAuthenticator;
 import org.securityfilter.filter.SecurityFilter;
 import org.securityfilter.filter.SecurityRequestWrapper;
+import org.securityfilter.realm.SimplePrincipal;
 
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.XWikiException;
@@ -65,6 +67,12 @@
         if (principal != null) {
             // login successful
             request.getSession().removeAttribute(LOGIN_ATTEMPTS);
+
+            // make sure the Principal contains wiki name information
+            if (!StringUtils.contains(principal.getName(), ':')) {
+                principal = new SimplePrincipal(context.getDatabase() + ":" + principal.getName());
+            }
+
             request.setUserPrincipal(principal);
             return false;
         } else {
@@ -95,10 +103,18 @@
             if (principal != null) {
                 // login successful
                 request.getSession().removeAttribute(LOGIN_ATTEMPTS);
+
+                // make sure the Principal contains wiki name information
+                if (!StringUtils.contains(principal.getName(), ':')) {
+                    principal = new SimplePrincipal(context.getDatabase() + ":" + principal.getName());
+                }
+
                 request.setUserPrincipal(principal);
+
                 return principal;
             }
         }
+
         return null;
     }
 
