Beispiel #1
0
        /// <summary>
        /// 取得Line會員資料
        /// </summary>
        /// <param name="lineAuthorize"></param>
        /// <returns></returns>
        public LineProfile GetLineAccount(LineAuthorizeModel lineAuthorize)
        {
            LineLoginProjectSetting lineSetting;

            if (true)
            {
                lineSetting = _line.LineLogin.RightTY;
            }
            LineTokenModel lineTokenModel = GetLineAccountToken(lineAuthorize, lineSetting);
            HttpClientHelper <LineProfile> httpClientHelper = new HttpClientHelper <LineProfile>(_httpClientFactory);

            httpClientHelper.SetHeaders("Authorization", "Bearer " + lineTokenModel.Access_Token);
            return(httpClientHelper.SendAsync(HttpMethod.Get, lineSetting.Profile.ProfileUri.ToString()));
        }
Beispiel #2
0
        /// <summary>
        /// 取得Line會員Access_Token
        /// </summary>
        /// <param name="lineAuthorize">line Authorize GetCode Model</param>
        /// <returns></returns>
        public LineTokenModel GetLineAccountToken(LineAuthorizeModel lineAuthorize, LineLoginProjectSetting lineSetting)
        {
            Dictionary <string, string> dic = new Dictionary <string, string>()
            {
                { "grant_type", lineSetting.Token.GrantType },
                { "code", lineAuthorize.Code },
                { "redirect_uri", lineSetting.Token.RedirectUri.ToString() },
                { "client_id", lineSetting.ClientId },
                { "client_secret", lineSetting.ClientSecret }
            };
            HttpClientHelper <LineTokenModel> httpClientHelper = new HttpClientHelper <LineTokenModel>(_httpClientFactory);
            LineTokenModel lineTokenModel = httpClientHelper.SendAsync(HttpMethod.Post, lineSetting.Token.TokenUri.ToString(), dic);

            return(lineTokenModel);
        }
Beispiel #3
0
        public ActionResult CodeReturn(string code = "", string state = "")
        {
            //取得Code後要建立Token資料
            HttpCookie cookie = Request.Cookies["State"];

            if (state.Equals(cookie.Value.ToString()))
            {
                //HttpPost
                LineTokenModel tokenModel = new LineTokenModel()
                {
                    code          = code,
                    client_id     = ConfigurationManager.AppSettings["ClientID"],
                    client_secret = ConfigurationManager.AppSettings["ClientSecret"],
                    redirect_uri  = ConfigurationManager.AppSettings["RedirectUri"],
                    grant_type    = "authorization_code"
                };
                return(View(tokenModel));
            }
            return(Content("<H1>State不正確</H1>"));
        }
Beispiel #4
0
        public ActionResult Notification(LineTokenModel m)
        {
            string     result = "";
            string     token  = "";
            HttpCookie cookie = Request.Cookies["LineToken"];

            //如果沒取得LineToken的Code
            if (cookie == null || String.IsNullOrEmpty(cookie.Value))
            {
                using (HttpClient http = new HttpClient())
                {
                    //跟Line取得Token的code
                    http.BaseAddress = new Uri("https://notify-bot.line.me");
                    //HttpPost
                    var content = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair <string, string>("grant_type", m.grant_type),
                        new KeyValuePair <string, string>("redirect_uri", m.redirect_uri),
                        new KeyValuePair <string, string>("code", m.code),
                        new KeyValuePair <string, string>("client_id", m.client_id),
                        new KeyValuePair <string, string>("client_secret", m.client_secret),
                    });
                    using (HttpResponseMessage message = http.PostAsync("/oauth/token", content).Result)
                    {
                        if (message.StatusCode == HttpStatusCode.OK)
                        {
                            result = message.Content.ReadAsStringAsync().Result;
                            token  = JObject.Parse(result)["access_token"].ToString();
                        }
                        else
                        {
                            return(View("Error"));
                        }
                    }
                    cookie = new HttpCookie("LineToken", token);
                    Response.Cookies.Add(cookie);
                }
            }
            return(View());
        }