Reflector shows the implementation of Membership.GetCurrentUserName is:
private static string GetCurrentUserName()
{
if (HostingEnvironment.IsHosted)
{
HttpContext current = HttpContext.Current;
if (current != null)
{
return current.User.Identity.Name;
}
}
IPrincipal currentPrincipal = Thread.CurrentPrincipal;
if ((currentPrincipal != null) && (currentPrincipal.Identity != null))
{
return currentPrincipal.Identity.Name;
}
return string.Empty;
}
At first glance the most likely explanation is that:
HttpContext.Current is not null, and
HttpContext.Current.User is null or has a null Identity property.
All other paths seem to have a test for null.
So I suggest you trace the type and contents of HttpContext.User.
HttpContext.Current.User is an IPrincipal, and most concrete implementations of IPrincipal that I know of don't allow a null identity, so I'd bet on HttpContext.User being null.