Advertisement
Guest User

Untitled

a guest
Apr 4th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using CMS.Helpers;
  2. using CMS.Membership;
  3. using CMS.SiteProvider;
  4. using Kentico.Content.Web.Mvc;
  5. using Kentico.Web.Mvc;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Security.Principal;
  11. using System.Web;
  12. using System.Web.Mvc;
  13. namespace KMVCHelper
  14. {
  15.     public class EnvironmentHelper
  16.     {
  17.         public static bool PreviewEnabled
  18.         {
  19.             get
  20.             {
  21.                 try
  22.                 {
  23.                     return HttpContext.Current.Kentico().Preview().Enabled;
  24.                 } catch(InvalidOperationException)
  25.                 {
  26.                     // This occurs only on the Owin Authentication calls due to the Dynamic route handler
  27.                     return false;
  28.                 }
  29.             }
  30.         }
  31.  
  32.         public static string CurrentCulture
  33.         {
  34.             get
  35.             {
  36.                 try {
  37.                     return HttpContext.Current.Kentico().Preview().CultureName;
  38.                 }
  39.                 catch (InvalidOperationException)
  40.                 {
  41.                     // This occurs only on the Owin Authentication calls due to the Dynamic route handler
  42.                     return "en-US";
  43.                 }
  44.             }
  45.         }
  46.  
  47.         public static string CurrentSiteName
  48.         {
  49.             get
  50.             {
  51.                 return CacheHelper.Cache<string>(cs =>
  52.                 {
  53.                     // do a lookup based on the URL
  54.  
  55.                     string SiteName = SiteInfoProvider.GetSites().WhereLike("SitePresentationUrl", "%" + HttpContext.Current.Request.Url.Host + "%").FirstOrDefault()?.SiteName;
  56.                     if (cs.Cached)
  57.                     {
  58.                         cs.CacheDependency = CacheHelper.GetCacheDependency("cms.site|byname|" + SiteName);
  59.                     }
  60.                     return SiteName;
  61.                 }, new CacheSettings(1440, "GetSiteFromUrl", HttpContext.Current.Request.Url.Host));
  62.             }
  63.         }
  64.  
  65.         public static UserInfo AuthenticatedUser(IPrincipal User)
  66.         {
  67.             string Username = (User != null && User.Identity != null ? User.Identity.Name : "public");
  68.             return CacheHelper.Cache<UserInfo>(cs =>
  69.             {
  70.                 return UserInfoProvider.GetUserInfo(Username);
  71.             }, new CacheSettings(CacheHelper.CacheMinutes(EnvironmentHelper.CurrentSiteName), "AuthenticatedUser", Username));
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement