Friday, February 24, 2012

SharePoint 2010 >> Possible ways to get logged in User Name & Handling Changes in FBA Users' Names if Membership Provider Name Changed

بسم الله الرحمن الرحيم 

You may needed, in many cases, to have the logged in user name in SharePoint 2010. And you may found all old FBA (Form Based Authentication) user names changed suddenly after you change the Membership Provider name. For this or that, you need to read this article.
To get the logged in user name you have the following methods available in SharePoint 2010 Web Part User Control:
  • this.Page.User.Identity.Name
  • HttpContext.Current.User.Identity.Name
  • SPContext.Current.Web.CurrentUser.LoginName
  • SPContext.Current.Web.CurrentUser.Name
Actually, there is also: "System.Web.Security.Membership.GetUser().UserName". But it gave me Exception with message: [Method or operation is not implemented]
However, I called each of the previous four properties for both windows (SharePoint System Account) and forms authentication. They returned with the following:
For "System Account" user with user called "user _name" and domain named "domain_name":
Property
Returned Value
Page.User.Identity.Name or
HttpContext.Current.User.Identity.Name
0#.w|domain_name\user_name
SPContext.Current.Web.CurrentUser.LoginName
SHAREPOINT\system
SPContext.Current.Web.CurrentUser.Name
System Account
For a Form Based Authenticated User called "fba_user" and Membership Provider called "membership_provider":
Property
Returned Value
Page.User.Identity.Name or
HttpContext.Current.User.Identity.Name
0#.f|membership_provider|fba_user
SPContext.Current.Web.CurrentUser.LoginName
0#.f|membership_provider|fba_user
SPContext.Current.Web.CurrentUser.Name
fba_user

But, the value for the last one (SPContext.Current.Web.CurrentUser.Name) could be changed to the format: 0#.f|membership_provider|fba_user, instantly, if you changed the membership provider named! This means that the user name will be ugly. Moreover, this ugly name will be shown in the ribbon!
However, get relaxed you can easily fix the name at your code; you can just extract the username from the long text. This can be easily done by:

public string GetFlatUserName()
{
    //First, be sure that the user is not anonymous user:
    if (SPContext.Current == null || SPContext.Current.Web.CurrentUser == null)
        return "Anonymous";
    //Second, parse it:
    else
    {
        string flatUserName = this.Page.User.Identity.Name;
        if (flatUserName.Contains("\\"))
        {
            flatUserName = flatUserName.Substring(flatUserName.IndexOf("\\") + 1);
        }
        else if (flatUserName.Contains("|"))
        {
            flatUserName = flatUserName.Substring(flatUserName.IndexOf("|") + 1);
        }
        return flatUserName;
    }
}
For more on fixing the ugly name shown in the Ribbon check: SharePoint 2010 Custom Welcome Control)

Kindly support me by giving some good feedback, adding some useful comments, share it at Facebook and/or make a +1 for this article at Google from the bottom of the page.
________________________________
(Muhammad Altabba)محمد الطباع 
SharePoint Developer with Project Management and Team Leadership Activities

1 comment: