Dynamic routes and Membership providers
Using the a Membership provider and the MVC framework, is it possible that routes are dynamically changed so that a already logged 开发者_如何学运维in user goes to his own page, rather than the default.
At the moment I go to the default. If the user is already logged in or not, there I do a redirect to their own page. This can't be the correct way! Can it?
In RegisterRoutes I have this
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
One option would be to use a Route Constraint.
public class AuthenticatedConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return httpContext.Request.IsAuthenticated;
}
}
Then you could define a "LoggedInDefault" route before the regular default:
routes.MapRoute(
"LoggedInDefault",
"{controller}/{action}/{id}",
new { controller = "LoggedIn", action = "Index", id = "" },
new { loggedIn= new AuthenticatedConstraint()}
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
精彩评论