Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a strange issue where when I call EnsureUser I get an error that the SharePoint user is not unique or cannot be found.

I have set up claims auth in 2010 using SQL membership provider. Got all the correct membership and profile entries in main site, CA and Security token web.configs.

I have the membership accounts created such that the email address is used for the username field as well as the email field.

The crazy thing is that for certain email addresses EnsureUser works fine but for others I get the error. I just can't work out why that would be.

Even if the code errors I am able to use the UI and manually add the FBA user using people picker with no error just fine.

The code which attempts to run EnsureUser is running in an elevated priv with correctly instantiated new SPSite and SPWeb objects and AllowUnSafeupdates set to true.

Any ideas?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Try to prefix the username with the membership provider info. It would look something like:

web.EnsureUser("i:0#.f|membershiprovidername|username");
share|improve this answer
    
Ahaa! I was actually doing that previously but removed it as I thought it was causing other issues. I used the following function in the end. public static string EncodeUsername(string username) { SPClaim claim = new SPClaim(SPClaimTypes.UserLogonName, username, "http://www.w3.org/2001/XMLSchema#string", SPOriginalIssuers.Format(SPOriginalIssuerType.Forms, GetMembershipProvider())); return SPClaimProviderManager.Local.EncodeClaim(claim); } –  PlanetWilson Sep 29 '12 at 7:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.