Wednesday, March 14, 2012

Junk Characters displayed in User Name

We created a custom membership and role provider for SharePoint 2010. The first time the user log’s in, the User Name is displayed in the format 0#.f|sqlmember|username

We observed that the junk characters are displayeded only the first time user logs ins. On subsequent login, the user name is properly displayed. Try with the out of box membership and role providers that ship with ASP.Net, and you will face the same issue.
Quick Fix to above problem: Use JavaScript on Page Load and hide the junk characters.

Below is some sample JavaScript/jQuery that one of my colleagues Vikram wrote, which you can further customize to your needs.

var welcomeObj = $("#zz15_Menu");
if (welcomeObj.length == 0)

        welcomeObj = $("#zz16_Menu");

if (welcomeObj.length != 0) {

     var userName = welcomeObj.children("SPAN").html();

        if (userName.indexOf("0#.f|sqlmember|") != -1) {

         var newUserName = welcomeObj.children("SPAN").html().replace("0#.f|sqlmember|", "");

            welcomeObj.children("SPAN").html(newUserName);

        }

    }

I consider it a quick fix. Its not guaranteed to work in future versions of SharePoint.

A more elegant solution will be to find out the root cause, fix it in the membership and role providers, or probably override the Welcome User name control. But if you are short of time like we were, the quick fix might come to your rescue.

The characters are not actually junk, as I have been wrongly calling them in my post so far. They represent the Claims in the encoded format.  Details of the claims encoding format are available on Wictor’s blog