예제 #1
0
        /// <inheritdoc />
        public ThirdPartyAuthorizeResult Authorize(AuthorizationInput input)
        {
            var tokenRequest = "https://api.weibo.com/oauth2/access_token" +
                               $"?client_id={AppId}&client_secret={AppSecret}&grant_type=authorization_code" +
                               $"&redirect_uri={RedirectUri}&code={input.Code}";
            var tokenResult = HttpRequester.Request(tokenRequest,
                                                    new HttpRequester.RequestOptions
            {
                Method = HttpRequester.HttpMethod.Post
            });
            var authorizeResult = JsonConvert.DeserializeObject <AuthorizeResult>(tokenResult);
            var accessToken     = authorizeResult.access_token;

            var uidRequest = "https://api.weibo.com/2/account/get_uid.json" +
                             $"?access_token={accessToken}";
            var uidResult = HttpRequester.Request(uidRequest, new HttpRequester.RequestOptions());
            var uid       = (string)JsonConvert.DeserializeObject <dynamic>(uidResult).uid;

            var thirdPartyUser = _thirdPartyUserRepository
                                 .GetAll()
                                 .FirstOrDefault(u => u.OpenId == uid);

            if (thirdPartyUser == null)
            {
                var userRequest = "https://api.weibo.com/2/users/show.json"
                                  + $"?access_token={accessToken}&uid={uid}";

                var userResult = HttpRequester.Request(userRequest, new HttpRequester.RequestOptions());
                var user       = JsonConvert.DeserializeObject <UserInfo>(userResult);
                thirdPartyUser = new ThirdPartyUser
                {
                    OpenId      = uid,
                    AccessToken = accessToken,
                    Name        = user.name,
                    NickName    = user.screen_name,
                    ThirdParty  = "Weibo"
                };
                _thirdPartyUserRepository.Insert(thirdPartyUser);
                CurrentUnitOfWork.SaveChanges();
            }
            thirdPartyUser.AccessToken = accessToken;
            CurrentUnitOfWork.SaveChanges();
            return(new ThirdPartyAuthorizeResult
            {
                ThirdPartyUser = new ThirdPartyUserOutput
                {
                    UserId = thirdPartyUser.UserId,
                    Name = thirdPartyUser.NickName,
                    NickName = thirdPartyUser.NickName
                },
                Token = $"OpenId={uid}&date={DateTime.Now:yyyy-MM-dd HH:mm:ss}&type=Weibo".EncryptQueryString(),
                Success = thirdPartyUser.UserId > 0,
                RequireCreateNewUser = thirdPartyUser.UserId == 0
            });
예제 #2
0
        /// <inheritdoc />
        public ThirdPartyAuthorizeResult Authorize(AuthorizationInput input)
        {
            IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do",
                                                     AppId, AppPrivateKey, "json", "1.0", "RSA2", AppPublicKey, "utf-8", false);
            AlipaySystemOauthTokenRequest tokenRequest = new AlipaySystemOauthTokenRequest
            {
                Code      = input.Code,
                GrantType = "authorization_code"
            };
            AlipaySystemOauthTokenResponse tokenResponse = client.Execute(tokenRequest);

            if (tokenResponse.IsError)
            {
                throw new UserFriendlyException("认证失败,请重试");
            }

            var thirdPartyUser = _thirdPartyUserRepository
                                 .GetAll()
                                 .FirstOrDefault(u => u.OpenId == tokenResponse.UserId);

            if (thirdPartyUser == null)
            {
                AlipayUserUserinfoShareRequest  userRequest  = new AlipayUserUserinfoShareRequest();
                AlipayUserUserinfoShareResponse userResponse = client.Execute(userRequest, tokenResponse.AccessToken);
                if (userResponse.IsError)
                {
                    throw new UserFriendlyException("认证失败,请重试");
                }
                thirdPartyUser = new ThirdPartyUser
                {
                    OpenId      = tokenResponse.UserId,
                    AccessToken = tokenResponse.AccessToken,
                    Name        = userResponse.RealName,
                    NickName    = userResponse.NickName,
                    ThirdParty  = "Alipay"
                };
                _thirdPartyUserRepository.Insert(thirdPartyUser);
                CurrentUnitOfWork.SaveChanges();
            }
            thirdPartyUser.AccessToken = tokenResponse.UserId;
            CurrentUnitOfWork.SaveChanges();
            return(new ThirdPartyAuthorizeResult
            {
                ThirdPartyUser = new ThirdPartyUserOutput
                {
                    UserId = thirdPartyUser.UserId,
                    Name = thirdPartyUser.NickName,
                    NickName = thirdPartyUser.NickName
                },
                Token = $"OpenId={tokenResponse.UserId}&date={DateTime.Now:yyyy-MM-dd HH:mm:ss}&type=Alipay".EncryptQueryString(),
                Success = thirdPartyUser.UserId > 0,
                RequireCreateNewUser = thirdPartyUser.UserId == 0
            });
예제 #3
0
        /// <inheritdoc />
        public ThirdPartyAuthorizeResult Authorize(AuthorizationInput input)
        {
            var tokenRequest = "https://api.weixin.qq.com/sns/oauth2/access_token" +
                               $"?appid={AppId}&secret={AppSecret}&code={input.Code}&grant_type=authorization_code";
            var tokenResult     = HttpRequester.Request(tokenRequest, new HttpRequester.RequestOptions());
            var authorizeResult = JsonConvert.DeserializeObject <AuthorizeResult>(tokenResult);
            var accessToken     = authorizeResult.access_token;
            var openId          = authorizeResult.openid;

            if (openId == null)
            {
                return(new ThirdPartyAuthorizeResult {
                    Success = false
                });
            }
            var thirdPartyUser = _thirdPartyUserRepository
                                 .GetAll()
                                 .FirstOrDefault(u => u.OpenId == openId);

            if (thirdPartyUser == null)
            {
                var userRequest = "https://api.weixin.qq.com/sns/userinfo" +
                                  $"?access_token={accessToken}&openid={openId}";
                var userResult = HttpRequester.Request(userRequest, new HttpRequester.RequestOptions());
                var user       = JsonConvert.DeserializeObject <UserInfo>(userResult);
                thirdPartyUser = new ThirdPartyUser
                {
                    OpenId      = openId,
                    AccessToken = accessToken,
                    Name        = user.nickname,
                    NickName    = user.nickname,
                    ThirdParty  = "Weixin"
                };
                _thirdPartyUserRepository.Insert(thirdPartyUser);
                CurrentUnitOfWork.SaveChanges();
            }
            thirdPartyUser.AccessToken = accessToken;
            CurrentUnitOfWork.SaveChanges();
            return(new ThirdPartyAuthorizeResult
            {
                ThirdPartyUser = new ThirdPartyUserOutput
                {
                    UserId = thirdPartyUser.UserId,
                    Name = thirdPartyUser.NickName,
                    NickName = thirdPartyUser.NickName
                },
                Token = $"OpenId={openId}&date={DateTime.Now:yyyy-MM-dd HH:mm:ss}&type=Weixin".EncryptQueryString(),
                Success = thirdPartyUser.UserId > 0,
                RequireCreateNewUser = thirdPartyUser.UserId == 0
            });
예제 #4
0
        /// <inheritdoc />
        public ThirdPartyAuthorizeResult Authorize(AuthorizationInput input)
        {
            var tokenRequest = "https://graph.qq.com/oauth2.0/token" +
                               $"?grant_type=authorization_code&client_id={AppId}&client_secret={AppKey}" +
                               $"&code={input.Code}&redirect_uri={RedirectUri}";
            var tokenResult   = HttpRequester.Request(tokenRequest, new HttpRequester.RequestOptions());
            var accessToken   = ParseAccessToken(tokenResult);
            var openIdRequest = "https://graph.qq.com/oauth2.0/me" +
                                $"?access_token={accessToken}";
            var openIdResult   = HttpRequester.Request(openIdRequest, new HttpRequester.RequestOptions());
            var openId         = ParseOpenId(openIdResult);
            var thirdPartyUser = _thirdPartyUserRepository
                                 .GetAll()
                                 .FirstOrDefault(u => u.OpenId == openId);

            if (thirdPartyUser == null)
            {
                var userRequest = "https://graph.qq.com/user/get_user_info" +
                                  $"?access_token={accessToken}&oauth_consumer_key={AppId}&openid={openId}";
                var userResult = HttpRequester.Request(userRequest, new HttpRequester.RequestOptions());
                var user       = JsonConvert.DeserializeObject <UserInfo>(userResult);
                thirdPartyUser = new ThirdPartyUser
                {
                    OpenId      = openId,
                    AccessToken = accessToken,
                    NickName    = user.nickname,
                    ThirdParty  = "QQ"
                };
                _thirdPartyUserRepository.Insert(thirdPartyUser);
                CurrentUnitOfWork.SaveChanges();
            }
            thirdPartyUser.AccessToken = accessToken;
            CurrentUnitOfWork.SaveChanges();
            return(new ThirdPartyAuthorizeResult
            {
                ThirdPartyUser = new ThirdPartyUserOutput
                {
                    UserId = thirdPartyUser.UserId,
                    Name = thirdPartyUser.NickName,
                    NickName = thirdPartyUser.NickName
                },
                Token = $"OpenId={openId}&date={DateTime.Now:yyyy-MM-dd HH:mm:ss}&type=QQ".EncryptQueryString(),
                Success = thirdPartyUser.UserId > 0,
                RequireCreateNewUser = thirdPartyUser.UserId == 0
            });