コード例 #1
0
        // Performs a Userinfo API call using OIDAuthState.withFreshTokensPerformAction.
        partial void Userinfo(UIButton sender)
        {
            var userinfoEndpoint = AuthState.LastAuthorizationResponse.Request.Configuration.DiscoveryDocument.UserinfoEndpoint;

            if (userinfoEndpoint == null)
            {
                Console.WriteLine($"Userinfo endpoint not declared in discovery document");
                return;
            }

            var currentAccessToken = AuthState.LastTokenResponse.AccessToken;

            Console.WriteLine($"Performing userinfo request");

            AuthState.PerformWithFreshTokens(async(accessToken, idToken, error) =>
            {
                if (error != null)
                {
                    Console.WriteLine($"Error fetching fresh tokens: {error.LocalizedDescription}");
                    return;
                }

                // log whether a token refresh occurred
                if (currentAccessToken != accessToken)
                {
                    Console.WriteLine($"Access token was refreshed automatically ({currentAccessToken} to {accessToken})");
                }
                else
                {
                    Console.WriteLine($"Access token was fresh and not updated {accessToken}");
                }

                // creates request to the userinfo endpoint, with access token in the Authorization header
                var httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                // performs HTTP request
                var response = await httpClient.GetAsync(userinfoEndpoint);
                var content  = await response.Content.ReadAsStringAsync();
                NSError deserializeError;
                var data = (NSDictionary)NSJsonSerialization.Deserialize(NSData.FromString(content), 0, out deserializeError);

                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine($"Success: {content}");

                    new UIAlertView("OpenID AppAuth", $"Hello, {data["name"]}!", null, "Hi").Show();
                }
                else
                {
                    // server replied with an error

                    if (response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        // "401 Unauthorized" generally indicates there is an issue with the authorization
                        // grant. Puts OIDAuthState into an error state.
                        var authError = ErrorUtilities.CreateResourceServerAuthorizationError(0, data, error);
                        AuthState.Update(authError);
                        // log error
                        Console.WriteLine($"Authorization Error ({authError}). Response: {content}");
                    }
                    else
                    {
                        // log error
                        Console.WriteLine($"HTTP Error ({response.StatusCode}). Response: {content}");
                    }
                }
            });
        }