Created
December 27, 2013 15:19
-
-
Save vinneyk/8148425 to your computer and use it in GitHub Desktop.
Early prototype of ClaimsAuthorizedNavigationMenuHelper. Create navigation menus with authorization trimming using Thinktecture's `ClaimsAuthorizeAttribute` and `ClaimsAuthorization` class. Dependends on Thinktecture.IdentityModel. Requires no configuration outside of the necessary Claims Auth setup. Please send comments and suggestions!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static class RouteHelpers | |
{ | |
public static string GetAreaName(this RouteBase route) | |
{ | |
return route.GetDataTokenValue("area"); | |
} | |
public static string GetControllerName(this RouteBase route) | |
{ | |
return route.GetDefaultValue("controller"); | |
} | |
public static string GetActionName(this RouteBase route) | |
{ | |
return route.GetDefaultValue("action"); | |
} | |
private static string GetDataTokenValue(this RouteBase route, string key) | |
{ | |
var castRoute = route as Route; | |
object rawValue = null; | |
if (castRoute != null && castRoute.DataTokens != null && castRoute.DataTokens.TryGetValue(key, out rawValue)) | |
{ | |
return rawValue as string; | |
} | |
return null; | |
} | |
private static string GetDefaultValue(this RouteBase route, string key) | |
{ | |
var castRoute = route as Route; | |
object rawValue = null; | |
if (castRoute != null && castRoute.Defaults != null && castRoute.Defaults.TryGetValue(key, out rawValue)) | |
{ | |
return rawValue as string; | |
} | |
return null; | |
} | |
public static Route GetRoute(this IEnumerable<RouteBase> routeCollection, string areaName, string controllerName, string actionName) | |
{ | |
var routes = String.IsNullOrWhiteSpace(areaName) | |
? routeCollection | |
: FilterRoutesByAreaName(routeCollection, areaName); | |
return (from routeBase in routes | |
let thisRoute = routeBase as Route | |
where thisRoute != null | |
&& thisRoute.GetControllerName().Equals(controllerName, StringComparison.OrdinalIgnoreCase) | |
&& thisRoute.GetActionName().Equals(actionName, StringComparison.OrdinalIgnoreCase) | |
select thisRoute).FirstOrDefault(); | |
} | |
public static IEnumerable<RouteBase> FilterRoutesByAreaName(IEnumerable<RouteBase> routes, string areaName) | |
{ | |
return from route in routes | |
let area = route.GetAreaName() ?? String.Empty | |
where String.Equals(areaName, area, StringComparison.OrdinalIgnoreCase) | |
select route; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment