Last post Feb 08, 2012 01:00 PM by sbetchel
Feb 08, 2012 01:00 PM|sbetchel|LINK
EDIT: I have solved this. I had to manually set the HttpContext.Current.User right after generating the page instance with CreateInstanceFromVirtualPath:
HttpContext.Current.User = new RolePrincipal(new FormsIdentity(new FormsAuthenticationTicket(user, true, 20)));
where 'user' was the user name extracted from the authentication ticket:
user = FormsAuthentication.Decrypt(HttpContext.Current.Request.Cookies[".ASPXAUTH"].Value).Name;
ive looked for days for a way to do this but can't seem to find a way. (I am not using MVC)
I am using FormsAuthentication and I have a LoginView control on a master page showing which user is logged in. On a normal aspx page request, it works fine. My problem is that I wrote a custom route handler that rewrites url requests to /content/X to 'workbench.aspx?page=X'
where X is the name of the content to display from a database. so www.site.com/workbench.aspx?page=foo becomes www.site.com/content/foo.
When this route occurs, the LoginView control on the master page that workbench.aspx is a child of no longer shows the user, but instead shows [Log in], as if they are not logged in. The authentication cookie is there, but Page.User is not being set, which
I believe LoginView relies on to display the user. Here is the code in the route, where you can see I am using CreateInstanceFromVirtualPath to return the handler. workbench.aspx then reads HttpContext.Current.Items["page"] to get the content used from the
database. Using this method fails to set Page.User which I think is my problem. Thanks in advance!
public class WorkbenchRouteHandler : IRouteHandler
#region IRouteHandler Members
public IHttpHandler GetHttpHandler(RequestContext requestContext)
string page = requestContext.RouteData.Values["page"] as string;
return new PageNotFoundHandler();
HttpContext.Current.Items["page"] = page;
System.Web.UI.Page p = System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(
"~/Workbench.aspx", typeof(System.Web.UI.Page)) as System.Web.UI.Page;