Esempio n. 1
0
        internal Cookie CreateWordPressCookie(ClaimsPrincipal principal)
        {
            var authClaim = principal.Claims.FirstOrDefault(x => x.Type == CLAIM_AUTH);

            if (authClaim != null && !string.IsNullOrWhiteSpace(authClaim.Value))
            {
                return(new Cookie(WordPressCookieAuthenticationHandler.GetWordPressCookieName(_options.Url), authClaim.Value, "/", new Uri(_options.Url).Host));
            }
            else
            {
                throw new NotImplementedException();
            }
            //var authCookie = new AuthCookie() { UserName = principal.Identity.Name };

            //// let's give sufficient time to time for the cookie, say 5 minutes
            //authCookie.ExpirationUtc = DateTime.UtcNow.AddMinutes(5);



            //// create the cookie

            //// cookie has form:  Name|Expiration|Hash;


            //// TODO: generate a proper auth token
            //DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
            //TimeSpan span = DateTime.Now.AddMinutes(10).Subtract(epoch);
            //long expiration = Convert.ToInt64(span.TotalSeconds);
            //existingCookie = new KeyValuePair<string, string>(wordPressAuthCookieName, I);


            //_logger.Warning("Unable to set authorization cookie as one was not able to be retrieved from HTTP context nor was one able to be create from current user");
        }
Esempio n. 2
0
        private Cookie TrySetWordPressCookiesFromHttpContext(CookieContainer cookieContainer, Scheme?scheme = null)
        {
            Cookie returnValue = null;

            string cookieName = null;

            if (scheme.HasValue)
            {
                switch (scheme.Value)
                {
                case Scheme.AUTH:
                    cookieName = "wordpress_";
                    break;

                case Scheme.SECURE_AUTH:
                    cookieName = "wordpress_sec_";
                    break;

                case Scheme.LOGGED_IN:
                    cookieName = "wordpress_logged_in_";
                    break;
                }
            }

            if (string.IsNullOrWhiteSpace(cookieName))
            {
                if (_options.IS_SSL)
                {
                    cookieName = "wordpress_sec_";
                    scheme     = Scheme.SECURE_AUTH;
                }
                else
                {
                    cookieName = "wordpress_";
                    scheme     = Scheme.AUTH;
                }
            }

            Uri wordPressUrl    = new Uri(_options.Url);
            var existingCookies = _httpContextAccessor?.HttpContext?.Request?.Cookies;

            if (existingCookies != null && existingCookies.Any())
            {
                string realAuthCookieName = WordPressCookieAuthenticationHandler.GetWordPressCookieName(_options.Url);
                if (!existingCookies.Any(x => x.Key == realAuthCookieName))
                {
                    realAuthCookieName = null;
                }
                if (string.IsNullOrWhiteSpace(realAuthCookieName))
                {
                    var possibleAuthCookies = existingCookies.Where(x => x.Key.StartsWith(cookieName, StringComparison.OrdinalIgnoreCase)).ToList();
                    if (possibleAuthCookies.Any())
                    {
                        if (possibleAuthCookies.Count == 1)
                        {
                            realAuthCookieName = possibleAuthCookies.Single().Key;
                        }
                        else
                        {
                            // we'll take the one with the smallest length
                            int len = possibleAuthCookies.Min(x => x.Key.Length);
                            realAuthCookieName = possibleAuthCookies.First(x => x.Key.Length == len).Key;
                        }
                    }
                }

                foreach (var c in existingCookies)
                {
                    var newCookie = new Cookie(c.Key, c.Value);
                    cookieContainer.Add(wordPressUrl, newCookie);
                    if (c.Key == realAuthCookieName)
                    {
                        returnValue = newCookie;
                    }
                }
            }
            return(returnValue);
        }