Пример #1
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);
        }
Пример #2
0
        /// <summary>
        /// 获取Authoration URL并跳转
        /// </summary>
        /// <param name="platform"></param>
        /// <param name="scope"></param>
        public virtual void BeginAuthoration(string platform, string scope = "")
        {
            var config = AuthConfigManager.GetConfigByPlatform(platform);

            if (config == null)
            {
                var err = Errors.NotSupportedPlatform(platform);
                Log.Debug(typeof(OAuthLogin), err);

                throw err;
            }
            if (config.Enabled)
            {
                var authUrl = CreateAuthorationUrlByConfig(platform, scope, config);
                HttpContext.Current.Response.Redirect(authUrl, true);
            }
            else
            {
                var err = Errors.OAuthLoginNotEnabled(platform);
                Log.Debug(typeof(OAuthLogin), err);

                throw err;
            }
        }