NuGet for Enterprise and MVC Routing

May 21, 2011 at 12:36 AM
Edited May 21, 2011 at 12:43 AM

If you have missed Scott's session at TechEd, Channel 9 posted it here: http://channel9.msdn.com/Events/TechEd/NorthAmerica/2011/DEV338

For those interested to see how we did the routing:

===

ServiceRoute NuGet_DefaultRoute = new ServiceRoute(
        "nuget/v1.0",
        factory,
        typeof(NuGetPackages));

NuGet_DefaultRoute.Defaults = new RouteValueDictionary { 
        { "serviceType", "odata" } 
};
             
NuGet_DefaultRoute.Constraints = new RouteValueDictionary { { "serviceType", "odata" } };

RouteTable.Routes.Add(null, NuGet_DefaultRoute);

Route NuGet_OAuthRoute = new Route(
        "{access_token}/nuget/v1.0/{*pathInfo}",
        new NuGetRouteHandler(NuGet_DefaultRoute)
);
             
RouteTable.Routes.Add(null, NuGet_OAuthRoute);
====
 
        public class NuGetRouteHandler : IRouteHandler {
            ServiceRoute _Route;

            public NuGetRouteHandler(ServiceRoute route) {
                _Route = route;
            }

            IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext) {
                IToken token = AccessTokenCache.GetTokenEx(requestContext.RouteData.Values["access_token"] as String);

                if (token == null) {
                    
                    HttpContext.Current.User = new GenericPrincipal(
                        new GenericIdentity(String.Empty),
                        null);

                } else {

                    MixedAuthenticationTicket ticket = MixedAuthenticationTicket.FromJson(token.AuthenticationTicket);
                    HttpContext.Current.User = new MixedAuthenticationPrincipal(
                        new MixedAuthenticationIdentity(
                            ticket.Identity,
                            ticket.Email,
                            ticket.Principal,
                            ticket.AuthenticationType,
                            ticket.FullName,
                            ticket.PictureUrl));
                }
                
                Thread.CurrentPrincipal = HttpContext.Current.User;

                requestContext.HttpContext.RewritePath(
                            String.Format("~/nuget/v1.0/{0}", requestContext.RouteData.Values["pathInfo"]), false);

                return _Route.RouteHandler.GetHttpHandler(requestContext);
            }
        }
=====
 
the NuGet OData Service uses a query interceptor to check for valid lic.
 
        [QueryInterceptor("Packages")]
        public Expression> Authenticate() {
            MixedAuthenticationIdentity me = 
                HttpContext.Current.User != null ? HttpContext.Current.User.Identity as MixedAuthenticationIdentity : null;
            
            return (p) => HasLicense(me, p);
        }
 
--
Azret - DevExpress