void IHttpModule.Init(HttpApplication context)
 {
     if (CdnHelper.IsCdnEnabled())
     {
         context.ReleaseRequestState += ReleaseRequestState;
     }
 }
Esempio n. 2
0
        public async Task <ChatUser> GetChatUser(string userId)
        {
            try
            {
                var chatuser = await GetUserFromCache(userId);

                if (chatuser == null)
                {
                    if (userId == Guid.Empty.ToString())
                    {
                        return new ChatUser {
                                   Id = Guid.Empty.ToString(), Avatar = $"{CdnHelper.ImagePath()}/Content/Images/Avatar.png"
                        }
                    }
                    ;

                    using (var context = new ApplicationDbContext())
                    {
                        var user = await context.Users
                                   .Where(x => x.Id == userId)
                                   .Select(x => new
                        {
                            Id             = x.Id,
                            UserName       = x.UserName,
                            ChatHandle     = x.ChatHandle,
                            ChatBanEndTime = x.ChatBanEndTime,
                            RoleCss        = x.RoleCss,
                            KarmaTotal     = x.KarmaTotal,
                        }).FirstOrDefaultNoLockAsync();

                        if (user == null)
                        {
                            return new ChatUser {
                                       Id = Guid.Empty.ToString(), Avatar = $"{CdnHelper.ImagePath()}/Content/Images/Avatar.png"
                            }
                        }
                        ;

                        chatuser = new ChatUser
                        {
                            Id         = user.Id,
                            UserName   = HttpUtility.HtmlEncode(user.UserName),
                            ChatHandle = HttpUtility.HtmlEncode(user.ChatHandle),
                            BanEndTime = user.ChatBanEndTime,
                            RoleCss    = user.RoleCss,
                            Avatar     = GetAvatar(user.UserName),
                            KarmaTotal = user.KarmaTotal
                        };
                        await AddUserToCache(userId, chatuser);
                    }
                }
                return(chatuser);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Esempio n. 3
0
        private string GetAvatar(string userName)
        {
            string filename = string.Format("{0}\\{1}.png", _avatarPath, userName);
            string avatar   = File.Exists(filename)
                                ? $"{CdnHelper.ImagePath()}/Content/Images/Avatar/{userName}.png"
                                : $"{CdnHelper.ImagePath()}/Content/Images/Avatar.png";

            return(avatar);
        }
    static void ReleaseRequestState(object sender, EventArgs e)
    {
        var response = HttpContext.Current.Response;
        var rawUrl   = HttpContext.Current.Request.RawUrl.ToLower();

        if (!CdnHelper.IsCdnAllowed(response, rawUrl))
        {
            return;
        }
        response.Filter = new CdnFilter(HttpContext.Current.Response.Filter, HttpContext.Current.Request);
    }
Esempio n. 5
0
 public static string GetAvatar(this IIdentity identity)
 {
     if (identity != null && identity.IsAuthenticated)
     {
         string serverPath = HostingEnvironment.MapPath("~/Content/Images/Avatar");
         string filename   = string.Format("{0}\\{1}", serverPath, identity.Name);
         if (File.Exists(filename + ".png"))
         {
             return(string.Format("{0}/Content/Images/Avatar/{1}.png", CdnHelper.ImagePath(), identity.Name));
         }
     }
     return(CdnHelper.ImagePath() + "/Content/Images/Avatar.png");
 }
        /// <summary>
        /// Determies if a CDN URL should be prefixed to the path
        /// </summary>
        /// <param name="path">The path to be prefixed</param>
        public string CdnPrefix(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(string.Empty);
            }
            if (!CdnHelper.IsCdnEnabled())
            {
                return(path);
            }
            var response = HttpContext.Current.Response;
            var rawUrl   = HttpContext.Current.Request.RawUrl.ToLower();

            if (!CdnHelper.IsCdnAllowed(response, rawUrl))
            {
                return(path);
            }
            var cdnFilter = new CdnFilter(HttpContext.Current.Response.Filter, HttpContext.Current.Request);
            var cdnPath   = cdnFilter.CdnParser.GetUrl(path);

            cdnFilter.ForceClose = true;
            cdnFilter.Close();
            return(cdnPath);
        }
Esempio n. 7
0
        public static async Task Run([TimerTrigger("0 17 23 * * *")] TimerInfo myTimer, ILogger log, ExecutionContext executionContext)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            string subscriptionId = Environment.GetEnvironmentVariable("SubscriptionId");
            var    config         = new ConfigurationBuilder()
                                    .SetBasePath(executionContext.FunctionAppDirectory)
                                    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                                    .AddEnvironmentVariables()
                                    .Build();

            var certificateDetails = new List <CertificateRenewalInputModel>();

            config.GetSection("CertificateDetails").Bind(certificateDetails);

            foreach (var certifcate in certificateDetails)
            {
                log.LogInformation($"Processing certificate - {certifcate.DomainName}");
                var acmeHelper        = new AcmeHelper(log);
                var certificateHelper = new KeyVaultCertificateHelper(certifcate.KeyVaultName);

                await InitAcme(log, certifcate, acmeHelper);

                string domainName = certifcate.DomainName;
                if (domainName.StartsWith("*"))
                {
                    domainName = domainName.Substring(1);
                }
                log.LogInformation($"Calculated domain name is {domainName}");

                string keyVaultCertificateName = domainName.Replace(".", "");
                log.LogInformation($"Getting expiry for {keyVaultCertificateName} in Key Vault certifictes");
                var certificateExpiry = await certificateHelper.GetCertificateExpiryAsync(keyVaultCertificateName);

                if (certificateExpiry.HasValue && certificateExpiry.Value.Subtract(DateTime.UtcNow).TotalDays > 7)
                {
                    log.LogInformation("No certificates to renew.");
                    continue;
                }

                log.LogInformation("Creating order for certificates");

                await acmeHelper.CreateOrderAsync(certifcate.DomainName);

                log.LogInformation("Authorization created");

                await FetchAndCreateDnsRecords(log, subscriptionId, certifcate, acmeHelper, domainName);

                log.LogInformation("Validating DNS challenge");

                await acmeHelper.ValidateDnsAuthorizationAsync();

                log.LogInformation("Challenge validated");

                string password = Guid.NewGuid().ToString();
                var    pfx      = await acmeHelper.GetPfxCertificateAsync(password, certifcate.CertificateCountryName, certifcate.CertificateState, certifcate.CertificateLocality,
                                                                          certifcate.CertificateOrganization, certifcate.CertificateOrganizationUnit, certifcate.DomainName, domainName);

                log.LogInformation("Certificate built");

                (string certificateName, string certificateVerison) = await certificateHelper.ImportCertificate(keyVaultCertificateName, pfx, password);

                log.LogInformation("Certificate imported");

                var cdnHelper = new CdnHelper(subscriptionId);
                await cdnHelper.EnableHttpsForCustomDomain(certifcate.CdnResourceGroup, certifcate.CdnProfileName,
                                                           certifcate.CdnEndpointName, certifcate.CdnCustomDomainName, certificateName, certificateVerison, certifcate.KeyVaultName);

                log.LogInformation("HTTPS enabling started");
            }
        }