3

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?

1 Answer 1

6

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

web.EnsureUser("i:0#.f|membershiprovidername|username");
1
  • 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); } Sep 29, 2012 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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