· 9 years ago · Oct 24, 2016, 06:16 PM
1// Contained in the OAuth Handler object
2public override async Task<bool> InvokeAsync()
3{
4 if (Options.CallbackPath.HasValue && Options.CallbackPath == Request.Path)
5 {
6 AuthenticationTicket ticket = await AuthenticateAsync();
7 }
8 return false;
9}
10
11protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
12{
13 AuthenticationProperties properties = null;
14 ClaimsIdentity identity = new ClaimsIdentity(Options.SignInAsAutenticationType);
15
16 try
17 {
18 string state = GetChallengeResponseState();
19 string temporaryToken = GetChallengeTemporaryToken();
20
21 properties = Options.StateDataFormat.Unprotect(state);
22 if (properties == null)
23 {
24 return null;
25 }
26
27 if (!ValidateCorrelationId(properties, _logger))
28 {
29 return new AuthenticationTicket(null, properties);
30 }
31
32 // This call results in the Exception being thrown
33 // replacing this with using (HttpClient client = new HttpClient()) and making await client.PostAsync(...); functions as expected here
34 OAuthToken accessToken = await GetOAuthTokenFromTemporaryToken(temporaryToken);
35 OrionApi api = new OrionApi(accessToken as AuthToken);
36 UserProfile user = await api.Authorization.UserAsync();
37 }
38 catch (Exception ex)
39 {
40 _logger.WriteError("Authentication failed", ex);
41 return new AuthenticationTicket(null, properties);
42 }
43
44 throw new NotImplementedException();
45}
46
47private async Task<OAuthToken> GetOAuthTokenFromTemporaryToken(string temporaryToken)
48{
49 string requestPrefix = Request.Scheme + "://" + Request.Host;
50 string redirectUri = requestPrefix + Request.PathBase + Options.CallbackPath;
51 return await SecurityEndpoint.PostOAuthTemporaryTokenAsync(
52 temporaryToken,
53 redirectUri,
54 Options.ClientId,
55 Options.ClientSecret,
56 Options.UseTestApiEndpoint
57 ); // tried adding .ConfigureAwait(false)
58}
59
60// Contained in the SecurityEndpoint object
61internal static async Task<OAuthToken> PostOAuthTemporaryTokenAsync(string temporaryToken, string redirectUri, string clientId, string clientSecret, bool useTestApi)
62{
63 SecurityEndpoint tempSecurityEndpoint = new SecurityEndpoint(useTestApi);
64 var parameters = new NameValueCollection
65 {
66 { "grant_type", "authorization_code" },
67 { "code", temporaryToken },
68 { "redirect_uri", redirectUri },
69 { "client_id", clientId },
70 { "client_secret", clientSecret },
71 };
72 var oauthTokenJson = await tempSecurityEndpoint.PostJsonAsync("Token", "", parameters); // Tried adding .ConfigureAwait(false)
73
74 return oauthTokenJson.ToObject<OAuthToken>();
75}
76
77// Contained in SecurityEndpoint's abstract base class
78protected async Task<JToken> PostJsonAsync(string endpointMethod, object body, NameValueCollection endpointParameters = null)
79{
80 using (HttpClient httpClient = BuildHttpClient())
81 {
82 HttpContent postContent;
83 if (body is HttpContent)
84 {
85 postContent = (HttpContent)body;
86 }
87 else
88 {
89 postContent = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
90 }
91 httpClient.Timeout = TimeSpan.FromSeconds(120);
92 var response = await httpClient.PostAsync(EndpointUri(endpointMethod, endpointParameters), postContent); // Tried adding .ConfigureAwait(false);
93
94 response.EnsureSuccessStatusCode();
95 return await ParseJsonResponseAsync(response);
96 }
97}
98
99at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
100 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
101 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
102 at OrionApiSdk.ApiEndpoints.Abstract.ApiEndpointBase.<PostJsonAsync>d__31.MoveNext() in E:Visual StudioProjectsDunhamConciergeOrionSignOnOrionApiSdkApiEndpointsAbstractApiEndpointBase.cs:line 130
103--- End of stack trace from previous location where exception was thrown ---
104 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
105 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
106 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
107 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
108 at OrionApiSdk.ApiEndpoints.Security.SecurityEndpoint.<PostOAuthTemporaryTokenAsync>d__13.MoveNext() in E:Visual StudioProjectsDunhamConciergeOrionSignOnOrionApiSdkApiEndpointsSecuritySecurityEndpoint.cs:line 92
109--- End of stack trace from previous location where exception was thrown ---
110 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
111 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
112 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
113 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()