Beispiel #1
0
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            AuthenticationProperties authenticationProperties = null;

            try
            {
                var code  = Request.Query.Get("code");
                var state = Request.Query.Get("state");

                authenticationProperties = Options.StateDataFormat.Unprotect(state);
                if (authenticationProperties == null)
                {
                    return(null);
                }

                if (!ValidateCorrelationId(authenticationProperties, _logger))
                {
                    return(new AuthenticationTicket(null, authenticationProperties));
                }

                if (code == null)
                {
                    return(new AuthenticationTicket(null, authenticationProperties));
                }

                var accessTokenResult = await GetAccessTokenResult(code);

                var accessToken = accessTokenResult[QQConnectDefaults.AccessTokenField];
                if (string.IsNullOrWhiteSpace(accessToken))
                {
                    _logger.WriteError("access_token was not found");
                    return(new AuthenticationTicket(null, authenticationProperties));
                }

                var openIdResult = await GetOpenIdResult(accessToken);

                var openId = openIdResult.TryGetValue(QQConnectDefaults.OpenIdField);
                if (string.IsNullOrWhiteSpace(openId))
                {
                    _logger.WriteError("openid was not found");
                    return(new AuthenticationTicket(null, authenticationProperties));
                }

                var userInfoResult = await GetUserInfoResult(accessToken, openId);

                var identity = QQConncetHelper.BuildClaimsIdentity(Options.ClientId, Options.AuthenticationType, accessTokenResult, openIdResult, userInfoResult);

                return(new AuthenticationTicket(identity, authenticationProperties));
            }
            catch (Exception ex)
            {
                _logger.WriteError("Authentication failed", ex);
                return(new AuthenticationTicket(null, authenticationProperties));
            }
        }
        protected override async Task <AuthenticateResult> HandleRemoteAuthenticateAsync()
        {
            try
            {
                var code  = Request.Query["code"][0];
                var state = Request.Query["state"][0];

                var authenticationProperties = Options.StateDataFormat.Unprotect(state);
                if (authenticationProperties == null)
                {
                    return(null);
                }

                if (ValidateCorrelationId(authenticationProperties) == false)
                {
                    return(AuthenticateResult.Fail(""));
                }

                if (code == null)
                {
                    return(AuthenticateResult.Fail("code is null"));
                }

                var accessTokenResult = await GetAccessTokenResult(code);

                var accessToken = accessTokenResult[QQConnectDefaults.AccessTokenField];
                if (string.IsNullOrWhiteSpace(accessToken))
                {
                    return(AuthenticateResult.Fail("access_token was not found"));
                }

                var openIdResult = await GetOpenIdResult(accessToken);

                var openId = openIdResult.TryGetValue(QQConnectDefaults.OpenIdField);
                if (string.IsNullOrWhiteSpace(openId))
                {
                    return(AuthenticateResult.Fail("openid was not found"));
                }

                var userInfoResult = await GetUserInfoResult(accessToken, openId);

                var identity = QQConncetHelper.BuildClaimsIdentity(Options.ClientId, Options.AuthenticationScheme, accessTokenResult, openIdResult, userInfoResult);

                return(AuthenticateResult.Success(new AuthenticationTicket(new ClaimsPrincipal(identity), authenticationProperties, Options.AuthenticationScheme)));
            }
            catch (Exception ex)
            {
                return(AuthenticateResult.Fail(ex));
            }
        }