Ejemplo n.º 1
0
        public IHttpActionResult GetAuth(string type)
        {
            try
            {
                var request = Context.GetCurrentRequest();

                var config      = Utils.GetConfigInfo();
                var oAuthType   = OAuthType.Parse(type);
                var redirectUrl = request.GetQueryString("redirectUrl");
                if (string.IsNullOrEmpty(redirectUrl))
                {
                    redirectUrl = ApiUtils.GetHomeUrl();
                }

                var url = string.Empty;

                if (oAuthType == OAuthType.Weibo)
                {
                    var client = new WeiboClient(config.WeiboAppKey, config.WeiboAppSecret, redirectUrl);
                    url = client.GetAuthorizationUrl();
                }
                else if (oAuthType == OAuthType.Weixin)
                {
                    var client = new WeixinClient(config.WeixinAppId, config.WeixinAppSecret, redirectUrl);
                    url = client.GetAuthorizationUrl();
                }
                else if (oAuthType == OAuthType.Qq)
                {
                    var client = new QqClient(config.QqAppId, config.QqAppKey, redirectUrl);
                    url = client.GetAuthorizationUrl();
                }

                if (!string.IsNullOrEmpty(url))
                {
                    HttpContext.Current.Response.Redirect(url);
                }

                return(BadRequest("类型不正确"));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> GetAuth([FromRoute] string type, [FromQuery] GetAuthRequest request)
        {
            var oAuthType   = OAuthType.Parse(type);
            var host        = ApiUtils.GetHost(Request);
            var redirectUrl = request.RedirectUrl;

            if (string.IsNullOrEmpty(redirectUrl))
            {
                redirectUrl = ApiUtils.GetHomeUrl();
            }

            var url = string.Empty;

            if (oAuthType == OAuthType.Weixin)
            {
                var settings = await _loginManager.GetWeixinSettingsAsync();

                var client = new WeixinClient(settings.WeixinAppId, settings.WeixinAppSecret, host, redirectUrl);
                url = client.GetAuthorizationUrl();
            }
            else if (oAuthType == OAuthType.Qq)
            {
                var settings = await _loginManager.GetQqSettingsAsync();

                var client = new QqClient(settings.QqAppId, settings.QqAppKey, host, redirectUrl);
                url = client.GetAuthorizationUrl();
            }
            else if (oAuthType == OAuthType.Weibo)
            {
                var settings = await _loginManager.GetWeiboSettingsAsync();

                var client = new WeiboClient(settings.WeiboAppKey, settings.WeiboAppSecret, host, redirectUrl);
                url = client.GetAuthorizationUrl();
            }

            if (!string.IsNullOrEmpty(url))
            {
                return(Redirect(url));
            }

            return(this.Error("类型不正确"));
        }