protected override async Task <AuthenticationTicket> AuthenticateCoreAsync() { AuthenticationProperties properties = null; try { var code = Request.Query.Get("code"); var state = Request.Query.Get("state"); properties = Options.StateDataFormat.Unprotect(state); if (properties == null) { return(null); } if (!ValidateCorrelationId(properties, _logger)) { return(new AuthenticationTicket(null, properties)); } if (code == null) { return(new AuthenticationTicket(null, properties)); } var token = await InnerHandler.GetTokenAsync( code, Request.CallCancelled); if (string.IsNullOrWhiteSpace(token.AccessToken)) { _logger.WriteError("access_token was not found"); return(new AuthenticationTicket(null, properties)); } var openId = await InnerHandler.GetOpenIdAsync( token.AccessToken, Request.CallCancelled); if (string.IsNullOrWhiteSpace(openId.OpenId)) { _logger.WriteError("openid was not found"); return(new AuthenticationTicket(null, properties)); } var user = await InnerHandler.GetUserAsync( token.AccessToken, openId.OpenId, Request.CallCancelled); var identity = QQConnectProfile.BuildClaimsIdentity(Options.AuthenticationType, token, openId, user); return(new AuthenticationTicket(identity, properties)); } catch (Exception ex) { _logger.WriteError("Authentication failed", ex); return(new AuthenticationTicket(null, properties)); } }
protected override async Task <HandleRequestResult> HandleRemoteAuthenticateAsync() { try { var code = Request.Query["code"].ToString(); var state = Request.Query["state"].ToString(); var properties = Options.StateDataFormat.Unprotect(state); if (properties == null) { return(HandleRequestResult.Fail("The oauth state was missing or invalid.")); } if (ValidateCorrelationId(properties) == false) { return(HandleRequestResult.Fail("Correlation failed.")); } if (code == null) { return(HandleRequestResult.Fail("Code was not found.")); } var token = await InnerHandler.GetTokenAsync( code, Context.RequestAborted); if (string.IsNullOrWhiteSpace(token.AccessToken)) { return(HandleRequestResult.Fail("OAuth token endpoint failure.")); } var openId = await InnerHandler.GetOpenIdAsync( token.AccessToken, Context.RequestAborted); if (string.IsNullOrWhiteSpace(openId.OpenId)) { return(HandleRequestResult.Fail("openid was not found.")); } var user = await InnerHandler.GetUserAsync( token.AccessToken, openId.OpenId, Context.RequestAborted); var identity = QQConnectProfile.BuildClaimsIdentity(Scheme.Name, token, openId, user); var principal = new ClaimsPrincipal(identity); var ticket = new AuthenticationTicket(principal, properties, Scheme.Name); return(HandleRequestResult.Success(ticket)); } catch (Exception ex) { return(HandleRequestResult.Fail(ex)); } }