Example #1
0
        public FormData GetFormData()
        {
            OAuthQQConfig config    = QQCore.GetConfig();
            FormData      formDatum = new FormData();

            FormData.FormItem[] formItemArray = new FormData.FormItem[3];
            FormData.FormItem   formItem      = new FormData.FormItem();
            formItem.DisplayName = "AppId";
            formItem.Name        = "AppId";
            formItem.IsRequired  = true;
            formItem.Type        = FormData.FormItemType.text;
            formItem.Value       = config.AppId;
            formItemArray[0]     = formItem;
            FormData.FormItem formItem1 = new FormData.FormItem();
            formItem1.DisplayName = "AppKey";
            formItem1.Name        = "AppKey";
            formItem1.IsRequired  = true;
            formItem1.Type        = FormData.FormItemType.text;
            formItem1.Value       = config.AppKey;
            formItemArray[1]      = formItem1;
            FormData.FormItem formItem2 = new FormData.FormItem();
            formItem2.DisplayName = "验证内容";
            formItem2.Name        = "ValidateContent";
            formItem2.IsRequired  = true;
            formItem2.Type        = FormData.FormItemType.text;
            formItem2.Value       = config.ValidateContent;
            formItemArray[2]      = formItem2;
            formDatum.Items       = formItemArray;
            return(formDatum);
        }
Example #2
0
        public void SetFormValues(IEnumerable <KeyValuePair <string, string> > values)
        {
            KeyValuePair <string, string> keyValuePair = values.FirstOrDefault((KeyValuePair <string, string> item) => item.Key == "AppId");

            if (string.IsNullOrWhiteSpace(keyValuePair.Value))
            {
                throw new PluginException("AppId不能为空");
            }
            KeyValuePair <string, string> keyValuePair1 = values.FirstOrDefault((KeyValuePair <string, string> item) => item.Key == "AppKey");

            if (string.IsNullOrWhiteSpace(keyValuePair1.Value))
            {
                throw new PluginException("AppKey不能为空");
            }
            KeyValuePair <string, string> keyValuePair2 = values.FirstOrDefault((KeyValuePair <string, string> item) => item.Key == "ValidateContent");

            if (!string.IsNullOrWhiteSpace(keyValuePair2.Value))
            {
                string lower = keyValuePair2.Value.ToLower();
                if (!lower.StartsWith("<meta "))
                {
                    throw new PluginException("验证内容必须以meta标签开头");
                }
                if (!lower.EndsWith(" />"))
                {
                    throw new PluginException("验证内容必须以 /> 结尾");
                }
            }
            OAuthQQConfig config = QQCore.GetConfig();

            config.AppId           = keyValuePair.Value;
            config.AppKey          = keyValuePair1.Value;
            config.ValidateContent = keyValuePair2.Value;
            QQCore.SaveConfig(config);
        }
Example #3
0
        public OAuthUserInfo GetUserInfo(NameValueCollection queryString)
        {
            QOpenClient qzone    = null;
            var         verifier = queryString["code"];
            var         state    = queryString["state"];

            OAuthQQConfig qqconfig = QQCore.GetConfig();
            string        url      = string.Format(qqconfig.AuthorizeURL + "?grant_type=authorization_code&client_id={0}&state={2}&client_secret={3}&code={4}&redirect_uri={1}", qqconfig.AppId, ReturnUrl, state, qqconfig.AppKey, verifier);

            QQConnectConfig.SetCallBackUrl(ReturnUrl);
            qzone = new QOpenClient(qqconfig.AuthorizeURL, qqconfig.AppId, qqconfig.AppKey, verifier, state);
            OAuthUserInfo userInfo = null;

            if (qzone != null)
            {
                userInfo = new OAuthUserInfo();
                var currentUser = qzone.GetCurrentUser();
                userInfo.NickName = currentUser.Nickname;
                userInfo.RealName = currentUser.Nickname;
                if (!string.IsNullOrWhiteSpace(currentUser.Gender) && (currentUser.Gender == "男" || currentUser.Gender == "女"))
                {
                    userInfo.IsMale = currentUser.Gender == "男" ? true : false;
                }
                userInfo.OpenId = qzone.OAuthToken.OpenId;
            }
            return(userInfo);
        }
Example #4
0
        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="config"></param>
        public static void SaveConfig(OAuthQQConfig config)
        {
            string        sDirectory = Himall.Core.Helper.IOHelper.urlToVirtual(WorkDirectory) + "/QQ.config";
            XmlSerializer xml        = new XmlSerializer(typeof(OAuthQQConfig));
            MemoryStream  Stream     = new MemoryStream();

            xml.Serialize(Stream, config);

            byte[]       b       = Stream.ToArray();
            MemoryStream stream2 = new MemoryStream(b);

            Himall.Core.HimallIO.CreateFile(sDirectory, stream2, Core.FileCreateType.Create);
        }
Example #5
0
        public static void SaveConfig(OAuthQQConfig config)
        {
            FileStream fileStream = new FileStream(string.Concat(QQCore.WorkDirectory, "\\QQ.config"), FileMode.Create);

            try
            {
                (new XmlSerializer(typeof(OAuthQQConfig))).Serialize(fileStream, config);
            }
            finally
            {
                if (fileStream != null)
                {
                    ((IDisposable)fileStream).Dispose();
                }
            }
        }
Example #6
0
        public void CheckCanEnable()
        {
            OAuthQQConfig config = QQCore.GetConfig();

            if (string.IsNullOrWhiteSpace(config.AppId))
            {
                throw new PluginConfigException("未设置AppId");
            }
            if (string.IsNullOrWhiteSpace(config.AppKey))
            {
                throw new PluginConfigException("未设置AppKey");
            }
            if (string.IsNullOrWhiteSpace(config.AuthorizeURL))
            {
                throw new PluginConfigException("未设置授权地址(AuthorizeURL)");
            }
        }
Example #7
0
        public string GetOpenLoginUrl(string returnUrl)
        {
            Service.ReturnUrl = returnUrl;
            OAuthQQConfig config = QQCore.GetConfig();

            if (string.IsNullOrWhiteSpace(config.AppId))
            {
                throw new MissingFieldException("未配置AppId");
            }
            if (string.IsNullOrWhiteSpace(config.AppKey))
            {
                throw new MissingFieldException("未配置AppKey");
            }
            string str  = "test";
            string str1 = "get_user_info";
            string str2 = string.Concat(config.AuthorizeURL, "?response_type=code&client_id={0}&redirect_uri={1}&scope={2}&state={3}");

            object[] appId = new object[] { config.AppId, returnUrl, str1, str };
            return(string.Format(str2, appId));
        }
Example #8
0
        public string GetOpenLoginUrl(string returnUrl)
        {
            ReturnUrl = returnUrl;
            OAuthQQConfig qqconfig = QQCore.GetConfig();

            if (string.IsNullOrWhiteSpace(qqconfig.AppId))
            {
                throw new MissingFieldException("未配置AppId");
            }
            if (string.IsNullOrWhiteSpace(qqconfig.AppKey))
            {
                throw new MissingFieldException("未配置AppKey");
            }

            string state = "test";
            string scope = "get_user_info";
            string url   = string.Format(qqconfig.AuthorizeURL + "?response_type=code&client_id={0}&redirect_uri={1}&scope={2}&state={3}", qqconfig.AppId, returnUrl, scope, state);

            return(url);
        }
Example #9
0
        /// <summary>
        /// 获取配置
        /// </summary>
        /// <returns></returns>
        public static OAuthQQConfig GetConfig()
        {
            OAuthQQConfig config = new OAuthQQConfig();

            string sDirectory = Himall.Core.Helper.IOHelper.urlToVirtual(WorkDirectory) + "/QQ.config";

            if (HimallIO.ExistFile(sDirectory))
            {
                XmlSerializer xs  = new XmlSerializer(typeof(OAuthQQConfig));
                byte[]        b   = Himall.Core.HimallIO.GetFileContent(sDirectory);
                string        str = System.Text.Encoding.Default.GetString(b);
                MemoryStream  fs  = new MemoryStream(b);
                config = (OAuthQQConfig)xs.Deserialize(fs);
            }
            else
            {
                SaveConfig(config);
            }

            return(config);
        }
Example #10
0
        public void SetFormValues(IEnumerable <KeyValuePair <string, string> > values)
        {
            var appidItem = values.FirstOrDefault(item => item.Key == "AppId");

            if (string.IsNullOrWhiteSpace(appidItem.Value))
            {
                throw new PluginException("AppId不能为空");
            }

            var appKeyItem = values.FirstOrDefault(item => item.Key == "AppKey");

            if (string.IsNullOrWhiteSpace(appKeyItem.Value))
            {
                throw new PluginException("AppKey不能为空");
            }

            var validateContent = values.FirstOrDefault(item => item.Key == "ValidateContent");

            if (!string.IsNullOrWhiteSpace(validateContent.Value))//如果验证内容不为空,则该内容必须是<meta>节点
            {
                var lowerValidate = validateContent.Value.ToLower();
                if (!lowerValidate.StartsWith("<meta "))
                {
                    throw new PluginException("验证内容必须以meta标签开头");
                }
                if (!lowerValidate.EndsWith(" />"))
                {
                    throw new PluginException("验证内容必须以 /> 结尾");
                }
            }

            OAuthQQConfig oldConfig = QQCore.GetConfig();

            oldConfig.AppId           = appidItem.Value;
            oldConfig.AppKey          = appKeyItem.Value;
            oldConfig.ValidateContent = validateContent.Value;
            QQCore.SaveConfig(oldConfig);
        }
Example #11
0
        public OAuthUserInfo GetUserInfo(NameValueCollection queryString)
        {
            bool          flag;
            QOpenClient   qOpenClient = null;
            string        item        = queryString["code"];
            string        str         = queryString["state"];
            OAuthQQConfig config      = QQCore.GetConfig();
            string        str1        = string.Concat(config.AuthorizeURL, "?grant_type=authorization_code&client_id={0}&state={2}&client_secret={3}&code={4}&redirect_uri={1}");

            object[] appId = new object[] { config.AppId, Service.ReturnUrl, str, config.AppKey, item };
            string.Format(str1, appId);
            QQConnectConfig.SetCallBackUrl(Service.ReturnUrl);
            qOpenClient = new QOpenClient(config.AuthorizeURL, config.AppId, config.AppKey, item, str);
            OAuthUserInfo oAuthUserInfo = null;

            if (qOpenClient != null)
            {
                oAuthUserInfo = new OAuthUserInfo();
                User currentUser = qOpenClient.GetCurrentUser();
                oAuthUserInfo.NickName = currentUser.Nickname;
                oAuthUserInfo.RealName = currentUser.Nickname;
                if (string.IsNullOrWhiteSpace(currentUser.Gender))
                {
                    flag = true;
                }
                else
                {
                    flag = (currentUser.Gender == "男" ? false : !(currentUser.Gender == "女"));
                }
                if (!flag)
                {
                    oAuthUserInfo.IsMale = new bool?((currentUser.Gender == "男" ? true : false));
                }
                oAuthUserInfo.OpenId = qOpenClient.OAuthToken.OpenId;
            }
            return(oAuthUserInfo);
        }
Example #12
0
        public string GetValidateContent()
        {
            OAuthQQConfig config = QQCore.GetConfig();

            return(config.ValidateContent);
        }