예제 #1
0
        protected void Dispose(bool isDisposable)
        {
            if (isDisposable)
            {
                AuthStateManager.Clear();

                if (_instance != null)
                {
                    GC.SuppressFinalize(_instance);
                    GC.ReRegisterForFinalize(_instance);
                }
                GC.SuppressFinalize(this);
                GC.ReRegisterForFinalize(this);
            }
            GC.Collect();
        }
예제 #2
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);
        }
예제 #3
0
        /// <summary>
        /// 执行OAuth验证登陆
        /// </summary>
        /// <param name="code"></param>
        /// <param name="pageState"></param>
        /// <param name="loginCallback"></param>
        /// <param name="errorCallback"></param>
        public virtual OAuthLoginResult Login(string code, string pageState)
        {
            var oauthLoginResult = new OAuthLoginResult();

            try
            {
                var platform = AuthStateManager.GetPlatformByState(pageState);
                var config   = AuthConfigManager.GetConfigByPlatform(platform);
                if (config == null)
                {
                    oauthLoginResult.Error = Errors.NotConfigForPlatform(platform);
                    return(oauthLoginResult);
                }
                if (config.Enabled)
                {
                    AuthStateManager.RemoveState(HttpContext.Current.Session.SessionID, pageState);

                    RestRequest request = CreateTokenRequest(code, pageState, config);
                    var         content = Execute(config.ApiBaseUrl, request).Content;

                    oauthLoginResult.Config         = config;
                    oauthLoginResult.ServerResponse = content;

                    Log.Debug(typeof(OAuthLogin), content);
                    return(oauthLoginResult);
                }

                oauthLoginResult.Error = Errors.OAuthLoginNotEnabled(platform);
            }
            catch (Exception ex)
            {
                if (ex is OAuthException)
                {
                    Log.Debug(typeof(OAuthLogin), "Server Response:" + (ex as OAuthException).Response.Content);
                }
                Log.Debug(typeof(OAuthLogin), ex);
                oauthLoginResult.Error = ex;
            }
            return(oauthLoginResult);
        }