Exemplo n.º 1
0
        /// <summary>
        /// 回复1:获取Token
        /// </summary>
        /// <param name="requestMessage"></param>
        private ResponseMessageText GetResponseBy_1(IRequestMessageBase requestMessage)
        {
            var responseMessage = CreateResponseMessage <ResponseMessageText>();

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("获取用户Token!");
            sb.AppendLine(KeyStore.AccessToken);
            sb.AppendLine(string.Format("Token过期时间{0}",
                                        AccessTokenContainer.TryGetItem(KeyStore.AppId).AccessTokenExpireTime));
            responseMessage.Content = sb.ToString();

            return(responseMessage);
        }
Exemplo n.º 2
0
        public ActionResult GetCode(string appId, string returnUrl)
        {
            string code = HttpContext.Request.QueryString["Code"];

            if (string.IsNullOrEmpty(code))
            {
                return(Redirect("/Account/Authorize?returnUrl=" + returnUrl));
            }

            string appSecret = AccessTokenContainer.TryGetItem(appId).AppSecret;
            OAuthAccessTokenResult result = OAuthApi.GetAccessToken(appId, appSecret, code);

            if (result.errcode.ToString() == "请求成功")
            {
                string openId       = result.openid;
                string access_token = result.access_token;
                var    user         = OAuthApi.GetUserInfo(access_token, openId);
                if (user != null && Session["openid"] == null)
                {
                    Session["openid"]   = user.openid;
                    Session["nickname"] = user.nickname;
                    using (IDbConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Default"].ConnectionString))
                    {
                        var predicate = Predicates.Field <NBUser>(f => f.OpenId, Operator.Eq, openId);
                        var cnt       = conn.Count <NBUser>(predicate);
                        if (cnt == 0)
                        {
                            NBUser nbUser = new NBUser()
                            {
                                OpenId     = user.openid,
                                NickName   = user.nickname,
                                CreateDate = DateTime.Now
                            };
                            conn.Insert <NBUser>(nbUser);
                        }
                    }
                }
            }
            else
            {
                Response.Write(result.errmsg);
            }
            return(Redirect(returnUrl));
        }