I have a Custom Membership Provider setup for a SharePoint 2013 Web App. The Web App is set up for Multi Tenancy and hence the Custom Membership Provider has a dynamic connection string, but uses different DBs based on the tenant. I have added a User in Membership DB and I am getting the claim encoded user name properly, in the format i:0#.f|CustomMembershipProviderName|UserName. Now, I use the following code to add the user to SP.
using(SPSite site = new SPSite(url))
{
using(SPWeb web = site.OpenWeb())
{
SPUser user = web.EnsureUser("i:0#.f|CustomMembershipProviderName|UserName");
}
}
The web.EnsureUser line throws an exception saying user not found in a Layout Page, whereas, it returns a user in the Console App. I have tried using SPSecurity.RunWithElevatedPrivileges, web.AllowUnsafeUpdates and so on. I have also ensured, that the user (running the SP Layout page) has enough permissions on the required DBs also. But I have no idea, why, it is working in a Console App and not in a Layout Page in SP. Can anyone please help me to fix this? I am really frustrated, as I have been breaking my head on this from past few days. Any help will be highly appreciated.
Ven