Get rid of that Claims Token in SharePoint 2010

Every developer who is working with Claim Based Security knows about the LoginName problem. Within a ClaimBased environment the identifier of the underlying claims provider is set as prefix in front of the entire domain name.

So, in some situations you have to (or would like to) get just domain and username. Here comes the SPClaimProviderManager into play. By asking for the Local Instance you’re able to decode the LoginName, which in detail safely removes the Claims Identifier from the LoginName.

Here is a short sample of that.

using Microsoft.SharePoint.Administration.Claims;
 
public string GetPlainUserName(string fullUserName)
{
    if(!SPClaimProviderManager.IsEncodedClaim(fullUserName))
      return fullUserName;
    var claimsProviderManager = SPClaimProviderManager.Local;
 
    return claimsProviderManager.DecodeClaim(fullUserName).Value;
}

First I asked the IsEncodedClaim method whether the given LoginName is encoded or not. Only when the login Name is encoded I use the local instance to query the decoded LoginName .

It’s really easy to utilize the SPClaimProviderManager and ensure that only the plain LoginName is returned.

 

Technorati Tags: ,
DotNetKicks-DE Image
Published Mittwoch, 22. Juni 2011 16:10 von ThorstenHans
Abgelegt unter: , ,

Kommentare

Keine Kommentare

Kommentar abgeben

(verpflichtend) 
(verpflichtend) 
(optional)
(verpflichtend)