Ejemplo n.º 1
0
        public JsonResult Query(string tradeno, string alipayTradeNo)
        {
            /*DefaultAopClient client = new DefaultAopClient(Config.Gatewayurl, Config.AppId, Config.PrivateKey, "json", "2.0",
             *  Config.SignType, Config.AlipayPublicKey, Config.CharSet, false);*/
            AlipayTradeQueryModel model = new AlipayTradeQueryModel
            {
                OutTradeNo = tradeno,
                TradeNo    = alipayTradeNo
            };

            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();

            request.SetBizModel(model);

            var response = _alipayService.Execute(request);

            return(Json(response.Body));
        }
Ejemplo n.º 2
0
        protected override async Task <HandleRequestResult> HandleRemoteAuthenticateAsync()
        {
            // 第一步,处理工作
            AuthenticationProperties properties = null;
            var query = Request.Query;

            // 若用户禁止授权,则重定向后不会带上 auth_code 参数,仅会带上 state 参数
            var code  = query["auth_code"];
            var state = query["state"];

            properties = Options.StateDataFormat.Unprotect(state);
            if (properties == null)
            {
                return(HandleRequestResult.Fail("The oauth state was missing or invalid."));
            }

            // OAuth2 10.12 CSRF
            if (!ValidateCorrelationId(properties))
            {
                return(HandleRequestResult.Fail("Correlation failed."));
            }

            if (StringValues.IsNullOrEmpty(code))
            {
                return(HandleRequestResult.Fail("Code was not found."));
            }

            // 第二步,通过 Code 获取 Access Token
            AlipaySystemOauthTokenResponse resAccessToken = null;

            try
            {
                var alipaySystemOauthTokenRequest = new AlipaySystemOauthTokenRequest
                {
                    Code         = code,
                    GrantType    = "authorization_code",
                    RefreshToken = ""
                };

                resAccessToken = _alipayService.Execute(alipaySystemOauthTokenRequest);
            }
            catch (Exception)
            {
                throw;
            }
            if (resAccessToken.IsError)
            {
                throw new Exception("Error occur when getting access token from Alipay.");
            }

            var identity = new ClaimsIdentity(ClaimsIssuer);

            if (Options.SaveTokens)
            {
                var authTokens = new List <AuthenticationToken>
                {
                    new AuthenticationToken {
                        Name = "access_token", Value = resAccessToken.AccessToken
                    }
                };

                if (!string.IsNullOrEmpty(resAccessToken.RefreshToken))
                {
                    authTokens.Add(new AuthenticationToken {
                        Name = "refresh_token", Value = resAccessToken.RefreshToken
                    });
                }

                if (!string.IsNullOrEmpty(resAccessToken.ExpiresIn))
                {
                    if (int.TryParse(resAccessToken.ExpiresIn, NumberStyles.Integer, CultureInfo.InvariantCulture, out int value))
                    {
                        var expiresAt = Clock.UtcNow + TimeSpan.FromSeconds(value);
                        authTokens.Add(new AuthenticationToken
                        {
                            Name  = "expires_at",
                            Value = expiresAt.ToString("o", CultureInfo.InvariantCulture)
                        });
                    }
                }

                properties.StoreTokens(authTokens);
            }

            var ticket = await CreateTicketAsync(identity, properties, ConvertToOAuthTokenResponse(resAccessToken));

            if (ticket != null)
            {
                return(HandleRequestResult.Success(ticket));
            }
            else
            {
                return(HandleRequestResult.Fail("Failed to retrieve user information from remote server."));
            }
        }