Example #1
0
        /// <summary>
        /// 获取目录下所有配置的OAuth配置
        /// 目录为:如果未在.config文件AppSettings节定义OAuthLoginConfigPath,那么目录默认为~/Configs。
        /// <para>注意:目录配置请使用相对目录</para>
        /// </summary>
        /// <returns></returns>
        public static List <OAuthLoginConfig> GetAllLoginConfigs()
        {
            Check.IsWebEnvironment();

            List <OAuthLoginConfig> localConfigs = new List <OAuthLoginConfig>();
            var configPath = ConfigurationManager.AppSettings[RS.get(ResourceKey.SETTINGS_OAuthLoginConfigPath)];

            if (configPath == null)
            {
                configPath = "~/Configs";
            }
            configPath = HttpContext.Current.Server.MapPath(configPath);

            if (!Directory.Exists(configPath))
            {
                throw Errors.OAuthLoginConfigDirectoryNotExist(configPath);
            }
            var configs = Directory.GetFiles(configPath, "*OAuthConfig.config", SearchOption.TopDirectoryOnly);

            foreach (var file in configs)
            {
                try
                {
                    var c = OAuthLoginConfig.Parse(file);
                    localConfigs.Add(c);
                }
                catch (Exception ex)
                {
                    Log.Debug(typeof(AuthConfigManager), ex);
                }
            }

            return(localConfigs);
        }
Example #2
0
        /// <summary>
        /// 创建Token获取的RestRequest
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        private RestRequest CreateTokenRequest(string code, string state, OAuthLoginConfig config)
        {
            RestRequest request = null;

            if ("get".IsFullEqual(config.ApiTokenGetMothed))
            {
                StringBuilder queryString = new StringBuilder();
                int           index       = 0;
                foreach (var p in config.ApiTokenParams)
                {
                    var value = p.Value.Replace("{AppKey}", config.AppKey)
                                .Replace("{AppSecret}", config.AppSecret)
                                .Replace("[state]", state)
                                .Replace("[code]", code);

                    if (config.UrlEncode)
                    {
                        value = value.Replace("{CallBackUrl}", config.CallBackUrl.ToEncodeUrl());
                    }
                    else
                    {
                        value = value.Replace("{CallBackUrl}", config.CallBackUrl);
                    }
                    queryString.Append(p.Key + "=" + value);
                    if (++index < config.ApiTokenParams.Count)
                    {
                        queryString.Append("&");
                    }
                }
                var url = config.ApiTokenUrl + "?" + queryString.ToString();
                request = new RestRequest(url, Method.GET);
            }
            else if ("post".IsFullEqual(config.ApiTokenGetMothed))
            {
                request = new RestRequest(config.ApiTokenUrl, Method.POST);
                foreach (var p in config.ApiTokenParams)
                {
                    var value = p.Value.Replace("{AppKey}", config.AppKey)
                                .Replace("{AppSecret}", config.AppSecret)
                                .Replace("[state]", state)
                                .Replace("[code]", code);

                    if (config.UrlEncode)
                    {
                        value = value.Replace("{CallBackUrl}", config.CallBackUrl.ToEncodeUrl());
                    }
                    else
                    {
                        value = value.Replace("{CallBackUrl}", config.CallBackUrl);
                    }
                    request.AddParameter(p.Key, value, ParameterType.GetOrPost);
                }
            }
            return(request);
        }
        /// <summary>
        /// 创建Token获取的RestRequest
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        private RestRequest CreateTokenRequest(string code, string state, OAuthLoginConfig config)
        {
            RestRequest request = null;
            if ("get".IsFullEqual(config.ApiTokenGetMothed))
            {
                StringBuilder queryString = new StringBuilder();
                int index = 0;
                foreach (var p in config.ApiTokenParams)
                {
                    var value = p.Value.Replace("{AppKey}", config.AppKey)
                                .Replace("{AppSecret}", config.AppSecret)
                                .Replace("[state]", state)
                                .Replace("[code]", code);

                    if (config.UrlEncode)
                    {
                        value = value.Replace("{CallBackUrl}", config.CallBackUrl.ToEncodeUrl());
                    }
                    else
                    {
                        value = value.Replace("{CallBackUrl}", config.CallBackUrl);
                    }
                    queryString.Append(p.Key + "=" + value);
                    if (++index < config.ApiTokenParams.Count)
                    {
                        queryString.Append("&");
                    }
                }
                var url = config.ApiTokenUrl + "?" + queryString.ToString();
                request = new RestRequest(url, Method.GET);
            }
            else if ("post".IsFullEqual(config.ApiTokenGetMothed))
            {
                request = new RestRequest(config.ApiTokenUrl, Method.POST);
                foreach (var p in config.ApiTokenParams)
                {
                    var value = p.Value.Replace("{AppKey}", config.AppKey)
                                .Replace("{AppSecret}", config.AppSecret)
                                .Replace("[state]", state)
                                .Replace("[code]", code);

                    if (config.UrlEncode)
                    {
                        value = value.Replace("{CallBackUrl}", config.CallBackUrl.ToEncodeUrl());
                    }
                    else
                    {
                        value = value.Replace("{CallBackUrl}", config.CallBackUrl);
                    }
                    request.AddParameter(p.Key, value, ParameterType.GetOrPost);
                }
            }
            return request;
        }
        /// <summary>
        /// 将一个文件名为xmlFileName的配置文件转化到配置类
        /// </summary>
        /// <param name="xmlFileName"></param>
        /// <returns></returns>
        public static OAuthLoginConfig Parse(string xmlFileName)
        {
            var ret = new OAuthLoginConfig();
            if (!File.Exists(xmlFileName))
            {
                throw Errors.OAuthLoginConfigFileNotExist(xmlFileName);
            }

            var doc = XDocument.Load(xmlFileName);

            var displayIndex = doc.Descendants("DisplayIndex").Single();
            ret.DisplayIndex = int.Parse(displayIndex.Value);

            var enabled = doc.Descendants("Enabled").Single();
            ret.Enabled = bool.Parse(enabled.Value);

            var encord = doc.Descendants("UrlEncode").Single();
            ret.UrlEncode = bool.Parse(encord.Value);

            var platform = doc.Descendants("Platform").Single();
            ret.Platform = platform.Value;

            var appkey = doc.Descendants("AppKey").Single();
            ret.AppKey = appkey.Value;

            var appsecret = doc.Descendants("AppSecret").Single();
            ret.AppSecret = appsecret.Value;

            var baseurl = doc.Descendants("ApiBaseUrl").Single();
            ret.ApiBaseUrl = baseurl.Value;

            var callback = doc.Descendants("CallBackUrl").Single();
            ret.CallBackUrl = callback.Value;

            var authUrl = doc.Descendants("AuthorizeUrlTemplate").Single();
            ret.AuthorizeUrlTemplate = authUrl.Value;

            var tokenUrl = doc.Descendants("ApiTokenUrl").Single();
            ret.ApiTokenUrl = tokenUrl.Value;

            var tokenGetMethod = doc.Descendants("ApiTokenGetMothed").Single();
            ret.ApiTokenGetMothed = tokenGetMethod.Value;

            var list = doc.Descendants("ApiTokenParams").Elements();
            ret.ApiTokenParams = new Dictionary<string, string>();

            foreach (var item in list)
            {
                ret.ApiTokenParams.Add(item.Name.LocalName, item.Value);
            }
            return ret;
        }
Example #5
0
        /// <summary>
        /// 根据配置文件获取AuthorationUrl
        /// </summary>
        /// <param name="platform"></param>
        /// <param name="scope"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        private string CreateAuthorationUrlByConfig(string platform, string scope, OAuthLoginConfig config)
        {
            string state   = AuthStateManager.RequestState(HttpContext.Current.Session.SessionID, platform);
            string authUrl = config.AuthorizeUrlTemplate
                             .Replace("{AppKey}", config.AppKey)
                             .Replace("{AppSecret}", config.AppSecret)
                             .Replace("{ApiBaseUrl}", config.ApiBaseUrl)
                             .Replace("[state]", state)
                             .Replace("[time]", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                             .Replace("{scope}", scope);

            if (config.UrlEncode)
            {
                authUrl = authUrl.Replace("{CallBackUrl}", config.CallBackUrl.ToEncodeUrl());
            }
            else
            {
                authUrl = authUrl.Replace("{CallBackUrl}", config.CallBackUrl);
            }
            return(authUrl);
        }
Example #6
0
        /// <summary>
        /// 将一个文件名为xmlFileName的配置文件转化到配置类
        /// </summary>
        /// <param name="xmlFileName"></param>
        /// <returns></returns>
        public static OAuthLoginConfig Parse(string xmlFileName)
        {
            var ret = new OAuthLoginConfig();

            if (!File.Exists(xmlFileName))
            {
                throw Errors.OAuthLoginConfigFileNotExist(xmlFileName);
            }

            var doc = XDocument.Load(xmlFileName);

            var displayIndex = doc.Descendants("DisplayIndex").Single();

            ret.DisplayIndex = int.Parse(displayIndex.Value);

            var enabled = doc.Descendants("Enabled").Single();

            ret.Enabled = bool.Parse(enabled.Value);

            var encord = doc.Descendants("UrlEncode").Single();

            ret.UrlEncode = bool.Parse(encord.Value);

            var platform = doc.Descendants("Platform").Single();

            ret.Platform = platform.Value;

            var appkey = doc.Descendants("AppKey").Single();

            ret.AppKey = appkey.Value;

            var appsecret = doc.Descendants("AppSecret").Single();

            ret.AppSecret = appsecret.Value;

            var baseurl = doc.Descendants("ApiBaseUrl").Single();

            ret.ApiBaseUrl = baseurl.Value;

            var callback = doc.Descendants("CallBackUrl").Single();

            ret.CallBackUrl = callback.Value;

            var authUrl = doc.Descendants("AuthorizeUrlTemplate").Single();

            ret.AuthorizeUrlTemplate = authUrl.Value;

            var tokenUrl = doc.Descendants("ApiTokenUrl").Single();

            ret.ApiTokenUrl = tokenUrl.Value;

            var tokenGetMethod = doc.Descendants("ApiTokenGetMothed").Single();

            ret.ApiTokenGetMothed = tokenGetMethod.Value;

            var list = doc.Descendants("ApiTokenParams").Elements();

            ret.ApiTokenParams = new Dictionary <string, string>();

            foreach (var item in list)
            {
                ret.ApiTokenParams.Add(item.Name.LocalName, item.Value);
            }
            return(ret);
        }
Example #7
0
 /// <summary>
 /// 默认构造函数
 /// </summary>
 /// <param name="config"></param>
 /// <param name="accessTokenCallbackString"></param>
 public OAuthContextBase(OAuthLoginConfig config, string accessTokenCallbackString)
 {
     this._config = config;
     this._accessCallbackString = accessTokenCallbackString;
 }
        /// <summary>
        /// 根据配置文件获取AuthorationUrl
        /// </summary>
        /// <param name="platform"></param>
        /// <param name="scope"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        private string CreateAuthorationUrlByConfig(string platform, string scope, OAuthLoginConfig config)
        {
            string state = AuthStateManager.RequestState(HttpContext.Current.Session.SessionID, platform);
            string authUrl = config.AuthorizeUrlTemplate
                             .Replace("{AppKey}", config.AppKey)
                             .Replace("{AppSecret}", config.AppSecret)
                             .Replace("{ApiBaseUrl}", config.ApiBaseUrl)
                             .Replace("[state]", state)
                             .Replace("[time]", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                             .Replace("{scope}", scope);

            if (config.UrlEncode)
            {
                authUrl = authUrl.Replace("{CallBackUrl}", config.CallBackUrl.ToEncodeUrl());
            }
            else
            {
                authUrl = authUrl.Replace("{CallBackUrl}", config.CallBackUrl);
            }
            return authUrl;
        }