private SiteMapNode ReturnNodeIfAccessible(SiteMapNode node) { if (node != null && node.IsAccessibleToUser(HttpContext.Current)) { return(node); } return(null); }
public static bool CheckUrlAccess(string path) { if (!path.StartsWith("~")) { Uri uri = new Uri(HttpContext.Current.Request.Url, new Uri(path, UriKind.RelativeOrAbsolute)); path = uri.PathAndQuery; } SiteMapNode siteMapNode = new SiteMapNode(LoginUtil.urlAuthSiteProvider, "authCheck", path); return(siteMapNode.IsAccessibleToUser(HttpContext.Current)); }
//public static SiteMapNode FindSiteMapNodeFromPublicFacingUrl(this SiteMap siteMap, HttpContextBase httpContext) //{ // var publicFacingUrl = UrlPath.GetPublicFacingUrl(httpContext); // return FindSiteMapNodeFromUrl(siteMap, publicFacingUrl.PathAndQuery, publicFacingUrl.AbsolutePath, publicFacingUrl.Host, httpContext.CurrentHandler); //} //public static SiteMapNode FindSiteMapNodeFromUrl(this SiteMap siteMap, string relativeUrl, string relativePath, string hostName, IHttpHandler handler) //{ // SiteMapNode node = null; // // Try absolute match with querystring // var absoluteMatch = siteMapChildStateFactory.CreateUrlKey(relativeUrl, hostName); // node = FindSiteMapNodeFromUrlMatch(absoluteMatch); // // Try absolute match without querystring // if (node == null && !string.IsNullOrEmpty(relativePath)) // { // var absoluteMatchWithoutQueryString = siteMapChildStateFactory.CreateUrlKey(relativePath, hostName); // node = FindSiteMapNodeFromUrlMatch(absoluteMatchWithoutQueryString); // } // // Try relative match // if (node == null) // { // var relativeMatch = siteMapChildStateFactory.CreateUrlKey(relativeUrl, string.Empty); // node = FindSiteMapNodeFromUrlMatch(relativeMatch); // } // // Try relative match with ASP.NET handler querystring // if (node == null) // { // Page currentHandler = handler as Page; // if (currentHandler != null) // { // string clientQueryString = currentHandler.ClientQueryString; // if (clientQueryString.Length > 0) // { // var aspNetRelativeMatch = siteMapChildStateFactory.CreateUrlKey(relativePath + "?" + clientQueryString, string.Empty); // node = FindSiteMapNodeFromUrlMatch(aspNetRelativeMatch); // } // } // } // // Try relative match without querystring // if (node == null && !string.IsNullOrEmpty(relativePath)) // { // var relativeMatchWithoutQueryString = siteMapChildStateFactory.CreateUrlKey(relativePath, string.Empty); // node = FindSiteMapNodeFromUrlMatch(relativeMatchWithoutQueryString); // } // return node; //} public static SiteMapNode FindSiteMapNode(this SiteMap siteMap, string rawUrl) { if (rawUrl == null) { throw new ArgumentNullException(nameof(rawUrl)); } rawUrl = rawUrl.Trim(); if (rawUrl.Length == 0) { return(null); } // NOTE: If the URL passed is absolute, the public facing URL will be ignored // and the current URL will be the absolute URL that is passed. var publicFacingUrl = UrlPath.GetPublicFacingUrl(UrlPath.HttpContext); var currentUrl = new Uri(publicFacingUrl, rawUrl); //// Search the internal dictionary for the URL that is registered manually. //var node = FindSiteMapNodeFromUrl(currentUrl.PathAndQuery, currentUrl.AbsolutePath, currentUrl.Host, System.Web.HttpContext.Current); SiteMapNode node = null; // Search for the URL by creating a context based on the new URL and matching route values. if (node == null) { // Create a TextWriter with null stream as a backing stream // which doesn't consume resources using (var nullWriter = new StreamWriter(Stream.Null)) { // create a new http context using the node's URL instead of the current one. var currentUrlRequest = new HttpRequest(string.Empty, currentUrl.ToString(), currentUrl.Query); var currentUrlResponse = new HttpResponse(nullWriter); var currentUrlContext = new HttpContext(currentUrlRequest, currentUrlResponse); var currentUrlHttpContext = new HttpContextWrapper(currentUrlContext); // Find node for the passed-in URL using the new HTTP context. This will do a // match based on route values and/or query string values. node = FindSiteMapNodeFromMvc(siteMap, currentUrlHttpContext); } } // Check accessibility if (node == null || !node.IsAccessibleToUser()) { return(null); } return(node); }
protected virtual bool IsNodeAccessible(SiteMapNode rootNode) { if (!rootNode.IsAccessibleToUser()) return false; if (rootNode.ChildNodes.Any()) { var itemCount = rootNode.ChildNodes.Count - 1; for (var i = itemCount; i >= 0; i--) { var currentNode = rootNode.ChildNodes[i]; if (!IsNodeAccessible(currentNode)) rootNode.ChildNodes.RemoveAt(i); } } return true; }