Reputation: 13887
I'll explain what I'm trying to do first. I have a claims identifier being passed in, for example, 0e.t|saml provider|[email protected]
. I want to trim that to [email protected]
.
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|[email protected]
saml provider|[email protected]
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?
Upvotes: 2
Views: 4033
Reputation: 13887
sigh Somehow the i:
at the beginning of the claims string was being lopped off.
Should have read i:0e.t|saml provider|[email protected]
. Once that was added back, everything started functioning properly.
Upvotes: 2