public async Task <string> GetTokenAsync()
 {
     if (!this.token.IsValidAndNotExpiring)
     {
         this.token = await this.GetNewAccessToken();
     }
     return(token.AccessToken);
 }
        private async Task <Auth0Token> GetNewAccessToken()
        {
            var token         = new Auth0Token();
            var client        = new HttpClient();
            var client_id     = ClientId;
            var client_secret = ClientSecret;
            var clientCreds   = System.Text.Encoding.UTF8.GetBytes($"{client_id}:{client_secret}");

            client.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("Basic", System.Convert.ToBase64String(clientCreds));

            var postMessage = new Dictionary <string, string>();

            postMessage.Add("grant_type", "client_credentials");
            postMessage.Add("audience", "https://utgapi.com");
            //postMessage.Add("scope", "access_token");
            var request = new HttpRequestMessage(HttpMethod.Post, "https://tikisoft.auth0.com/oauth/token")
            {
                Content = new FormUrlEncodedContent(postMessage)
            };

            var response = await client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                var json = await response.Content.ReadAsStringAsync();

                token           = JsonConvert.DeserializeObject <Auth0Token>(json);
                token.ExpiresAt = DateTime.UtcNow.AddSeconds(this.token.ExpiresIn);
            }
            else
            {
                throw new ApplicationException("Unable to retrieve access token from Auth0");
            }

            return(token);
        }
Exemplo n.º 3
0
        public static void WriteSessionCookie(HttpApplication application, Auth0Token token)
        {
            var request = application.Context.Request;
            var response = application.Context.Response;

            var bytes = EncodeCookie(token);
            var cookie = Convert.ToBase64String(bytes);
            var chunkCount = cookie.Length / CookieChunkSize + (cookie.Length % CookieChunkSize == 0 ? 0 : 1);
            for (int i = 0; i < chunkCount; ++i)
            {
                var setCookie = new StringBuilder();
                setCookie.Append(LiveAuth);
                if (i > 0)
                {
                    setCookie.Append(i.ToString(CultureInfo.InvariantCulture));
                }

                setCookie.Append('=');

                int startIndex = i * CookieChunkSize;
                setCookie.Append(cookie.Substring(startIndex, Math.Min(CookieChunkSize, cookie.Length - startIndex)));
                setCookie.Append("; path=/");
                if (request.Url.Scheme == "https")
                {
                    setCookie.Append("; secure");
                }
                setCookie.Append("; HttpOnly");
                response.Headers.Add("Set-Cookie", setCookie.ToString());
            }

            var cookies = request.Cookies;
            var index = chunkCount;
            while (true)
            {
                var cookieName = LiveAuth;
                if (index > 0)
                {
                    cookieName += index.ToString(CultureInfo.InvariantCulture);
                }

                if (cookies[cookieName] == null)
                {
                    break;
                }

                // remove old cookie
                response.Headers.Add("Set-Cookie", String.Format(DeleteCookieFormat, cookieName));
                ++index;
            }
        }
Exemplo n.º 4
0
 public static byte[] EncodeCookie(Auth0Token token)
 {
     var bytes = token.ToBytes();
     for (int i = 0; i < DefaultCookieTransforms.Length; ++i)
     {
         bytes = DefaultCookieTransforms[i].Encode(bytes);
     }
     return bytes;
 }