I am having a very strange issue at the moment. I have a site written in asp.net 3.5. It has been hosted for ages on 1 server running IIS 6. Recently, I moved it to a new server running IIS7. The existing code base would not work.
If not logged in, User.Identity.Name was not empty like it used to be, but the User was null until logged in. So I change all the places from:
if (!String.IsNullOrEmpty(User.Identity.Name))
to
if (User != null && User.Identity != null && !String.IsNullOrEmpty(User.Identity.Name))
Which works fine in the most part. But I have found 2 pages, of all the pages of the site, that it is always null. blog/read.aspx for example. All other pages see the user as logged in. As there is a control on the master page that has login or logout controls depending on your status. But browsing to that page, it is as if the user is always logged off. With Please login to comment message. Browse away, and the user is logged in again. Browse back again and not logged in. It is like that page cannot read the authentication cookie for some reason. And I have no idea why.
FYI. It is Running on a ASP.NET 2.0 Integrated application pool. It does not work on a classic pipeline.
No idea why or what, but I would start by putting in a http module to trace out the value of the User variable at each stage in the pipeline. That can give you a vector on where it gets dumped at least.
Ok, I figured out that the problem is with the routing. If I browse to the page via a route eg. site.com/blogs/blog-title. User is always null. But if I browse to it like this: blogs/read.aspx?id=345 then all works fine.
Any ideas on how to fix this? This is how its setup:
Global
void RegisterRoutes(RouteCollection routes)
{
routes.Add("Blog", new Route("blogs/{blog}", new RouteHandler.BlogsRouteHandler()));
}
public class BlogsRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string blog = requestContext.RouteData.Values["blog"] as string;
if (String.IsNullOrEmpty(blog))
{
return RedirectErrorNotFound();
}
HttpContext.Current.Items["blog"] = blog;
return BuildManager.CreateInstanceFromVirtualPath("~/blogs/read.aspx", typeof(Page)) as Page;
}
}