I am getting an error while execute any Sharepoint Object model code. FBA ebabled Site is configured with CostumMembership & Roleprovider. There is no login page. HTTPModule is used to authenticate (AD) from different server
Once authenticated, in post authentication method it is calling through following code to generate token.User id is taken from HTTPContext. But throwing error while executing SPSite
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.SharePoint.SPSecurity.GetRolesForUser(String roleManagerName, String userName, UInt32& roleCount, String& roles)
But it is working when we call the Sharepoint site url directly
without HTTPContext / authenticated from other server.
using (SPSite s = new SPSite(r.Url.AbsoluteUri,))
{ SPIisSettings iis = s.WebApplication.IisSettings[SPUrlZone.Default];
SPFormsAuthenticationProvider fap = iis.FormsClaimsAuthenticationProvider;
SecurityToken token = SPSecurityContext.SecurityTokenForFormsAuthentication(new Uri(r.Url.GetComponents(UriComponents.Scheme
| UriComponents.HostAndPort, UriFormat.Unescaped), UriKind.Absolute), fap.MembershipProvider, fap.RoleProvider, username, cookieValue);
Microsoft.SharePoint.IdentityModel.SPFederationAuthenticationModule fam = Microsoft.SharePoint.IdentityModel.SPFederationAuthenticationModule.Current;
fam.SetPrincipalAndWriteSessionToken(token);
}