Question

I'll explain what I'm trying to do first. I have a claims identifier being passed in, for example, 0e.t|saml provider|first.last@domain.local. I want to trim that to first.last@domain.local.

I am well aware that this can be done with simple string formatting, however, this is not very flexible, so if something gets passed in that I don't expect, the string formatting could fail. It's simply more prone to issues.

I want to do this dynamically instead. Here's what I've tried.

Attempting to EnsureUser with the claims identifier above, then calling SPUser.Name:

SPUser spUser = SPContext.Current.Web.EnsureUser(spUserName);
userName = spUser.Name;

I've tried the following strings as a parameter in EnsureUser, all result in an exception: The user with the specified logonName cannot be found in Active Directory.

0e.t|saml provider|first.last@domain.local

saml provider|first.last@domain.local

first.last@domain.local

Again, all of those fail.

Another approach I tried, using SPClaimProviderManager (pulled from this blog):

SPClaimProviderManager mgr = SPClaimProviderManager.Local;
if (mgr != null && SPClaimProviderManager.IsEncodedClaim(userName))
{
    spUserName = mgr.DecodeClaim(SPContext.Current.Web.CurrentUser.LoginName).Value;
}

I want to ensure that I'm attempting to decode an actual claim, and not something else, so I call IsEncodedClaim. This, however, always returns false.

My questions:

1) What am I doing wrong here that results in both of these attempting failing? What do I need to do differently for them to function properly?

2) Is there a better method to get a friendly claims user name without string parsing?

Was it helpful?

Solution

sigh Somehow the i: at the beginning of the claims string was being lopped off.

Should have read i:0e.t|saml provider|first.last@domain.local. Once that was added back, everything started functioning properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top