/// <summary>Flush</summary>
        public override void Flush()
        {
            byte[] bb = TempBuffer.GetBuffer();

            if (bb != null && bb.Length > 0)
            {
                // 書き換え処理
                Encoding enc     = Response.ContentEncoding;
                string   content = enc.GetString(bb);

                // JSON形式なので、JsonConvertでaccess_tokenを抜き出す。
                Dictionary <string, object> accessTokenResponse = JsonConvert.DeserializeObject <Dictionary <string, object> >(content);

                // access_tokenを
                if (accessTokenResponse.ContainsKey("access_token"))
                {
                    string access_token = (string)accessTokenResponse["access_token"];
                    string id_token     = OpenIDConnectModule.ChangeToIdTokenFromJwt(access_token);
                    if (!string.IsNullOrEmpty(id_token))
                    {
                        // responseにid_tokenとして、このJWTを追加する。
                        accessTokenResponse.Add("id_token", id_token);
                        bb = enc.GetBytes(JsonConvert.SerializeObject(accessTokenResponse));
                    }
                }
            }

            ResponseStream.Write(bb, 0, bb.Length);
            ResponseStream.Flush();
        }
Example #2
0
        /// <summary>OnPreSendRequestHeaders</summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        private void OnPreSendRequestHeaders(object sender, EventArgs e)
        {
            // PreSendRequestHeadersのロジックはここに挿入

            HttpApplication application = (HttpApplication)sender;
            HttpContext     context     = application.Context;
            HttpResponse    response    = context.Response;

            if (context.Request.Url.AbsolutePath.IndexOf(
                    ASPNETIdentityConfig.OAuthAuthorizeEndpoint) != -1)
            {
                bool a = (context.Request.QueryString["response_type"].ToLower() == "id_token token");
                bool b = (context.Request.QueryString["response_type"].ToLower() == "id_token");

                if (a || b)
                {
                    // OpenID Connect : [response_type=id_token token] or [response_type=id_token]に対応

                    //レスポンス内容を参照して書き換え
                    string location = response.Headers["Location"];

                    if (!string.IsNullOrEmpty(location) &&
                        location.IndexOf("#access_token=") != -1)
                    {
                        // ・正規表現でaccess_tokenを抜き出す。
                        string pattern      = "(\\#access_token=)(?<accessToken>.+?)(\\&)";
                        string access_token = Regex.Match(location, pattern).Groups["accessToken"].Value;
                        string id_token     = OpenIDConnectModule.ChangeToIdTokenFromJwt(access_token);

                        if (!string.IsNullOrEmpty(id_token))
                        {
                            // responseにid_tokenとして、このJWTを追加する。
                            if (a)
                            {
                                response.Headers["Location"] = location + "&id_token=" + id_token;
                            }
                            else if (b)
                            {
                                location = location.Replace("access_token=" + access_token + "&", "");
                                location = location.Replace("token_type=beara" + access_token + "&", "");
                                response.Headers["Location"] = location + "&id_token=" + id_token;
                            }
                        }
                    }
                }
            }
        }