private static void HandleTokenResponse(HttpContext context, OAuthToken token, OAuthError error)
        {
            Dictionary<string, string> cookieValues = new Dictionary<string, string>();
            HttpCookie cookie = context.Request.Cookies[wlCookie];
            HttpCookie newCookie = new HttpCookie(wlCookie);
            newCookie.Path = "/";
            newCookie.Domain = context.Request.Headers["Host"];

            if (cookie != null && cookie.Values != null)
            {
                foreach (string key in cookie.Values.AllKeys)
                {
                    newCookie[key] = cookie[key];
                }
            }

            if (token != null)
            {
                JsonWebToken userInfo = ReadUserInfoFromAuthToken(token);
                // The userInfo contains identifiable information about the user.
                // You may add some logic here.

                newCookie[OAuthConstants.AccessToken] = HttpUtility.UrlEncode(token.AccessToken);
                newCookie[OAuthConstants.AuthenticationToken] = HttpUtility.UrlEncode(token.AuthenticationToken);
                newCookie[OAuthConstants.Scope] = HttpUtility.UrlPathEncode(token.Scope);
                newCookie[OAuthConstants.ExpiresIn] = HttpUtility.UrlEncode(token.ExpiresIn);

                if (!string.IsNullOrEmpty(token.RefreshToken))
                {
                    SaveRefreshToken(token.RefreshToken);
                }
            }

            if (error != null)
            {
                newCookie[OAuthConstants.Error] = HttpUtility.UrlEncode(error.Code);
                newCookie[OAuthConstants.ErrorDescription] = HttpUtility.UrlPathEncode(error.Description);
            }

            context.Response.Cookies.Add(newCookie);
        }
Exemple #2
0
        private static void HandleTokenResponse(HttpContext context, OAuthToken token, OAuthError error)
        {
            Dictionary<string, string> cookieValues = new Dictionary<string, string>();
            HttpCookie cookie = context.Request.Cookies[wlCookie];
            HttpCookie newCookie = new HttpCookie(wlCookie);
            newCookie.Path = "/";
            newCookie.Domain = context.Request.Headers["Host"];

            if (cookie != null && cookie.Values != null)
            {
                foreach (string key in cookie.Values.AllKeys)
                {
                    newCookie[key] = cookie[key];
                }
            }

            if (token != null)
            {
                newCookie[OAuthConstants.AccessToken] = HttpUtility.UrlEncode(token.AccessToken);
                newCookie[OAuthConstants.Scope] = HttpUtility.UrlPathEncode(token.Scope);
                newCookie[OAuthConstants.ExpiresIn] = HttpUtility.UrlEncode(token.ExpiresIn);

                if (!string.IsNullOrEmpty(token.RefreshToken))
                {
                    SaveRefreshToken(token.RefreshToken);
                }
            }

            if (error != null)
            {
                newCookie[OAuthConstants.Error] = HttpUtility.UrlEncode(error.Code);
                newCookie[OAuthConstants.ErrorDescription] = HttpUtility.UrlPathEncode(error.Description);
            }

            context.Response.Cookies.Add(newCookie);
        }
        private static void RequestAccessTokenByVerifier(string verifier, out OAuthToken token, out OAuthError error)
        {
            string content = String.Format("client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&grant_type=authorization_code",
                HttpUtility.UrlEncode(clientId),
                HttpUtility.UrlEncode(callback),
                HttpUtility.UrlEncode(clientSecret),
                HttpUtility.UrlEncode(verifier));

            RequestAccessToken(content, out token, out error);
        }
        private static JsonWebToken ReadUserInfoFromAuthToken(OAuthToken token)
        {
            string authenticationToken = token.AuthenticationToken;
            Dictionary<int, string> keys = new Dictionary<int, string>();
            keys.Add(0, clientSecret);

            JsonWebToken jwt = null;
            try
            {
                jwt = new JsonWebToken(authenticationToken, keys);
                return jwt;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        private static void RequestAccessToken(string postContent, out OAuthToken token, out OAuthError error)
        {
            token = null;
            error = null;

            HttpWebRequest request = WebRequest.Create(oauthUrl) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
                
            try
            {
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(postContent);
                }

                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                if (response != null)
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(OAuthToken));
                    token = serializer.ReadObject(response.GetResponseStream()) as OAuthToken;
                    if (token != null)
                    {
                        return;
                    }
                }
            }
            catch (WebException e)
            {
                HttpWebResponse response = e.Response as HttpWebResponse;
                if (response != null)
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(OAuthError));
                    error = serializer.ReadObject(response.GetResponseStream()) as OAuthError;
                }
            }
            catch (IOException)
            {
            }

            if (error == null)
            {
                error = new OAuthError("request_failed", "Failed to retrieve user access token.");
            }
        }
 private static void RequestAccessTokenByRefreshToken(string refreshToken, out OAuthToken token, out OAuthError error)
 {
     string content = String.Format("client_id={0}&redirect_uri={1}&client_secret={2}&refresh_token={3}&grant_type=refresh_token",
         HttpUtility.UrlEncode(clientId),
         HttpUtility.UrlEncode(callback),
         HttpUtility.UrlEncode(clientSecret),
         HttpUtility.UrlEncode(refreshToken));
     RequestAccessToken(content, out token, out error);
 }
        private void HandleTokenResponse(HttpContext context, OAuthToken token, OAuthError error)
        {
            NameValueCollection nvc = new NameValueCollection();

            if (token != null)
            {
                nvc[OAuthConstants.AccessToken] = HttpUtility.UrlEncode(token.AccessToken);
                nvc[OAuthConstants.Scope] = HttpUtility.UrlPathEncode(token.Scope);
                nvc[OAuthConstants.ExpiresIn] = HttpUtility.UrlEncode(token.ExpiresIn);

                if (!string.IsNullOrEmpty(token.RefreshToken))
                {
                    SaveRefreshToken(token.RefreshToken);
                }
            }

            if (error != null)
            {
                nvc[OAuthConstants.Error] = HttpUtility.UrlEncode(error.Code);
                nvc[OAuthConstants.ErrorDescription] = HttpUtility.UrlPathEncode(error.Description);
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(clientId);
            sb.Append('?');
            bool hasAppeneded = false;

            foreach (string key in nvc.AllKeys)
            {
                if (hasAppeneded)
                {
                    sb.Append('&');
                }
                else
                {
                    hasAppeneded = true;
                }

                sb.Append(key);
                sb.Append('=');
                sb.Append(nvc[key]);
            }

            this.Response.Redirect(sb.ToString());
        }