示例#1
0
        private async Task StartFacebookFlow(SampleSettings settings)
        {
            var client = new FacebookOAuth2Client(settings.ToOAuth2ClientBuilder());

            // Get OAuth2 request URI
            var options = new FacebookOAuth2ClientCreateAuthorizationUriOption();

            options.Scope.Add("email");
            var requestUri = await client.CreateAuthorizationUriAsync(options);

            var f = new OAuth2Window(requestUri.AbsoluteUri, settings.RedirectUri);

            if (f.ShowDialog() != true)
            {
                return;
            }

            var returnedUri = f.ReturnedUri;

            this.PrintOutput("Redirect Uri: " + returnedUri.AbsoluteUri);

            var parameters = returnedUri.ParseQueryString();

            var state = parameters["state"];

            if (state != options.State)
            {
                this.PrintOutput("WARNING: State is not returned or does not match");
            }

            var error = parameters["error"];

            if (!string.IsNullOrEmpty(error))
            {
                this.PrintOutput("Error returned: " + error);
                return;
            }

            var code = parameters["code"];

            if (string.IsNullOrEmpty(code))
            {
                this.PrintOutput("No code in the returned uri.");
                return;
            }

            var accessToken = await client.RequestAccessTokenAsync(code);

            this.PrintOutput("Access Token: " + Environment.NewLine + JsonConvert.SerializeObject(accessToken));

            await client.RequestAppAccessTokenAsync();

            this.PrintOutput("App Access Token: " + client.AppAccessToken.AccessToken);

            var userInfo = await client.GetUserInfoAsync(accessToken.AccessToken);

            this.PrintOutput("User Info: " + Environment.NewLine + JsonConvert.SerializeObject(userInfo));

            this.PrintOutput("Done!");
        }
示例#2
0
 private void SaveSettings(SampleSettings settings)
 {
     Settings.Default.SampleSettings = JsonConvert.SerializeObject(settings);
     Settings.Default.Save();
 }
示例#3
0
        private async Task StartMicrosoftAsync(SampleSettings settings)
        {
            var client = new MicrosoftOAuth2Client(settings.ToOAuth2ClientBuilder());

            var options = new MicrosoftOAuth2ClientCreateAuthorizationUriOption();

            options.Scope.Add("openid");
            options.Scope.Add("email");
            options.Scope.Add("profile");
            options.Scope.Add("offline_access");
            options.Scope.Add("user.read");

            options.ResponseType = MicrosoftOperationResponseType.IdTokenAndCode;

            var requestUri = await client.CreateAuthorizationUriAsync(options);

            var f = new OAuth2Window(requestUri.AbsoluteUri, settings.RedirectUri);

            if (f.ShowDialog() != true)
            {
                return;
            }

            // Change the # into ?
            var returnedUri = new Uri(f.ReturnedUri.AbsoluteUri.Replace("#", "?"));

            this.PrintOutput("Redirect Uri: " + returnedUri.AbsoluteUri);

            var parameters = returnedUri.ParseQueryString();

            var state = parameters["state"];

            if (state != options.State)
            {
                this.PrintOutput("WARNING: State is not returned or does not match");
            }

            var error = parameters["error"];

            if (!string.IsNullOrEmpty(error))
            {
                this.PrintOutput("Error returned: " + error);
                return;
            }

            var code = parameters["code"];

            if (string.IsNullOrEmpty(code))
            {
                this.PrintOutput("No code in the returned uri.");
                return;
            }

            var authToken = await client.RequestAccessTokenAsync(code);

            this.PrintOutput($"Access Token: {authToken.AccessToken}; Refresh Token: {authToken.RefreshToken}");

            var accessToken = await client.RefreshAccessTokenAsync(authToken.RefreshToken);

            this.PrintOutput($"Access Token: {accessToken.AccessToken}");

            var userInfo = await client.GetUserInfoAsync(accessToken.AccessToken);

            this.PrintOutput("User Info: " + Environment.NewLine + JsonConvert.SerializeObject(userInfo));

            this.PrintOutput("Done!");
        }