Helper Method to Convert I:0#.w|DOMAIN\USERNAME to DOMAIN\USERNAME

Introduction

It's a very common problem we developers encounter when we upgrade our solution from a previous version of SharePoint to the latest 2013 version when you get encoded claims usernames (in other words i:0#.w|DOMAIN\USERNAME name instead of DOMAIN\USERNAME).

When executing the code this is what we get for the username value: i:0#.w|DOMAIN\USERNAME but to output our code we need something like DOMAIN\USERNAME.

Some Information Related to the Problem

However why does the username that is claims encoded look the way it does? Some way it helps us since the claims format tells us what type of claim it is. If you still want to see the provider settings you can see it by selecting Web Application in Central Administration.Navigate to the path shown below:

Application Management -> Manage Web Application -> Select your Web App - > In Ribbon Select Authentication Provider.

authentication providers

There you will see the current authentication provider for the web application you selected.

Solution

When searching for a solution I have seen it in many places where people are using methods like string-splits to eliminate the claims usernames and convert it to the default domain\username format. Hopefully the method below might save someone hours of work.

Please find the following code snippet that will satisfy your purpose.

  1.  /// <summary>    
  2. /// method to convert i:0#.w|DOMAIN\USERNAME to   DOMAIN\USERNAME     
  3. /// </summary>    
  4. /// <param name="claimsEncodedUsername">username encoded in  claims</param>    
  5. /// <returns> return the normal domain/username without any claims identification data</returns>    
  6. public static string GetUsernameFromClaim(string claimsEncodedUsername, SPUser spUser)    
  7. {    
  8.     string claimsUsername = claimsEncodedUsername;    
  9.     try    
  10.     {    
  11.         SPClaimProviderManager spClaimProviderManager = SPClaimProviderManager.Local;    
  12.         if (spClaimProviderManager != null)    
  13.         {    
  14.             if (spUser != null)    
  15.             {    
  16.                 SPClaim userClaim = spClaimProviderManager.ConvertSPUserToClaim(spUser);    
  17.                 return spClaimProviderManager.ConvertClaimToIdentifier(userClaim.ToEncodedString());    
  18.             }    
  19.         }    
  20.     }    
  21.     catch (Exception ex)    
  22.     {    
  23.         // just return the original username.    
  24.         return claimsUsername;    
  25.     }    
  26.     // Return the original username value if it couldn't be resolved as a claims username    
  27.     return claimsUsername;    
This is how you need to call the method:

GetUsernameFromClaim("i:0#.w|DOMAIN\USERNAME",spUser)

Rather than going for methods like String.Split() or others, we should go for something that is using OOB functions in the SharePoint API.

Happy SharePoint !!!!