Hey, if you are like me and use claims to login to your SharePoint, you probably noticed the user names went nuts.
Having a user name like domain\user will now return something more like i:0#.w|domain\user if you are lucky, but I’ve seen stranger formats as well…
Now, say a user sends you a login name (domain\user) and you need to get this value and set it to the assigned to of a task, or set permissions for this user – you will quickly find out you cannot get an SPUser object from this standard login name.
Also, when you want to send the login name to another system or work with it in your code – you might want to get just the domain\user part, without the claims decorations.
True, the claims login formats are documented and you can hope to parse the format yourself to extract the user login from it, but I recently found a hidden (to me at least) gem in SharePoint API that can help with this task.
First, start using this namespace: Microsoft.SharePoint.Administration.Claims;
Now, you can work with SPClaimProviderManager object to check if a string is a claims encoded user name, and convert it to a standard user name, and the other way around.
I’ve created this simple utility class for me to use in my code for now, here is a code sample explaining how to use this object:
(Code is for example purposes only, use at own risk, feel free to change the names of the helpers)
Notice I’m expecting to get a windows login user name (SPIdentifierTypes.WindowsSamAccountName) but you can change this to support your own authentication.
Good luck, hope this help.