private void SignIn(object obj) { string logoutUri = OneDriveClient.GetDefaultLogoutUri(); string authorizationUri = OneDriveClient.GetDefaultAuthorizationUri(); AuthenticationResult authenticationResult = null; BrowserAuthenticationWindow authWindow = new BrowserAuthenticationWindow(); authWindow.Browser.Navigated += (sender, args) => { if (string.Equals(args.Uri.AbsolutePath, "/oauth20_logout.srf", StringComparison.OrdinalIgnoreCase)) { // The logout page has finished loading, so we can now load the login page. authWindow.Browser.Navigate(authorizationUri); } // If the browser is directed to the redirect URI, the new URI will contain the access code that we can use to // get a token for OneDrive. if (string.Equals(args.Uri.AbsolutePath, "ietf:wg:oauth:2.0:oob", StringComparison.OrdinalIgnoreCase)) { // We were directed back to the redirect URI. Extract the code from the query string Dictionary <string, string> queryParametes = args.Uri.GetQueryParameters(); if (queryParametes.ContainsKey("code")) { authenticationResult = new AuthenticationResult() { Code = queryParametes["code"] }; } // All done. Close the window. authWindow.Close(); } }; authWindow.Closed += (sender, args) => { if (authenticationResult == null) { return; } this.CanSignIn = false; this.SignInMessage = "Working..."; this.Adapter.SignIn(authenticationResult).ContinueWith(t => this.SetSignInStatus()); }; authWindow.Loaded += (sender, args) => { authWindow.Browser.Navigate(logoutUri); NativeMethods.User32.SetForegroundWindow(new WindowInteropHelper(authWindow).Handle); }; authWindow.ShowDialog(); }
private void SignIn(object obj) { string state = RandomDataBase64Url(32); string codeVerifier = RandomDataBase64Url(32); string codeChallenge = Base64UrlencodeNoPadding(Sha256(codeVerifier)); const string CodeChallengeMethod = "S256"; // Creates a redirect URI using an available port on the loopback address. string redirectUri = string.Format("http://{0}/", IPAddress.Loopback); string[] scopes = { "openid", "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile" }; string authUrl = string.Format( "{0}?response_type=code&scope={6}&redirect_uri={1}&client_id={2}&state={3}&code_challenge={4}&code_challenge_method={5}", "https://accounts.google.com/o/oauth2/v2/auth", redirectUri, GoogleDriveClient.SyncProAppId, state, codeChallenge, CodeChallengeMethod, HttpUtility.UrlEncode(string.Join(" ", scopes))); AuthenticationResult authenticationResult = null; BrowserAuthenticationWindow authWindow = new BrowserAuthenticationWindow(); authWindow.Browser.Navigating += (sender, args) => { if (args.Uri.ToString().StartsWith(redirectUri, StringComparison.OrdinalIgnoreCase)) { var qsList = args.Uri.Query.TrimStart('?') .Split('&') .ToDictionary(k => k.Split('=').First(), k => k.Split('=').Skip(1).First()); authenticationResult = new AuthenticationResult { Code = qsList["code"] }; authWindow.Close(); } }; authWindow.Loaded += (sender, args) => { authWindow.Browser.Navigate(authUrl); NativeMethods.User32.SetForegroundWindow(new WindowInteropHelper(authWindow).Handle); }; authWindow.Closed += (sender, args) => { if (authenticationResult == null) { return; } this.CanSignIn = false; this.SignInMessage = "Working..."; this.Adapter.SignIn(authenticationResult, codeVerifier).ContinueWith(this.SignInComplete); }; //authWindow.Closing += (sender, e) => //{ //}; authWindow.ShowDialog(); /* * // Build the URI that will show the login/authorization page. * string authorizationUri = * string.Format( * "https://login.live.com/oauth20_authorize.srf?client_id={0}&scope={1}&response_type=code&redirect_uri={2}", * GoogleDriveClient.SyncProAppId, * HttpUtility.UrlEncode(string.Join(" ", "onedrive.readwrite", "wl.signin", "wl.offline_access", "wl.basic")), * GoogleDriveClient.DefaultReturnUri); * * //AuthenticationResult authenticationResult = null; * authWindow.Browser.Navigated += (sender, args) => * { * if (string.Equals(args.Uri.AbsolutePath, "/oauth20_logout.srf", StringComparison.OrdinalIgnoreCase)) * { * // The logout page has finished loading, so we can now load the login page. * authWindow.Browser.Navigate(authorizationUri); * } * * // If the browser is directed to the redirect URI, the new URI will contain the access code that we can use to * // get a token for OneDrive. * if (string.Equals(args.Uri.AbsolutePath, "ietf:wg:oauth:2.0:oob", StringComparison.OrdinalIgnoreCase)) * { * // We were directed back to the redirect URI. Extract the code from the query string * Dictionary<string, string> queryParametes = args.Uri.GetQueryParameters(); * * // TODO: WHat here?? * //authenticationResult = new AuthenticationResult() * //{ * // Code = queryParametes["code"] * //}; * * // All done. Close the window. * authWindow.Close(); * } * }; */ //authWindow.Browser.LoadCompleted += (sender, args) => //{ // if (string.Equals(args.Uri.AbsolutePath, "/oauth20_logout.srf", StringComparison.OrdinalIgnoreCase)) // { // // The logout page has finished loading, so we can now load the login page. // authWindow.Browser.Navigate(authorizationUri); // } //}; // TODO: Fix /* * authWindow.Closed += (sender, args) => * { * if (authenticationResult == null) * { * return; * } * * this.CanSignIn = false; * this.SignInMessage = "Working..."; * this.AdapterBase.SignIn(authenticationResult).ContinueWith(this.SignInComplete); * }; * * authWindow.Loaded += (sender, args) => * { * authWindow.Browser.Navigate(logoutUri); * NativeMethods.SetForegroundWindow(new WindowInteropHelper(authWindow).Handle); * }; * * authWindow.ShowDialog(); */ //await this.SignInToMicrosoftAccount(); //ODUserProfile profileInfo = await this.AdapterBase.GetUserProfileAsync(); //if (profileInfo == null) //{ // this.SignInMessage = "Not Signed In"; // this.IsSignedIn = false; //} //else //{ // this.SignInMessage = string.Format("Signed in as {0} ({1})", profileInfo.Name, profileInfo.Emails.Account); // this.IsSignedIn = true; //} }
public static void SignIn(string path) { tokenPath = path; string logoutUri = OneDriveClient.GetDefaultLogoutUri(); string authorizationUri = OneDriveClient.GetDefaultAuthorizationUri(); AuthenticationResult authenticationResult = null; BrowserAuthenticationWindow authWindow = new BrowserAuthenticationWindow(); authWindow.Browser.Navigated += (sender, args) => { if (string.Equals(args.Uri.AbsolutePath, "/oauth20_logout.srf", StringComparison.OrdinalIgnoreCase)) { // The logout page has finished loading, so we can now load the login page. authWindow.Browser.Navigate(authorizationUri); } // If the browser is directed to the redirect URI, the new URI will contain the access code that we can use to // get a token for OneDrive. if (string.Equals(args.Uri.AbsolutePath, "ietf:wg:oauth:2.0:oob", StringComparison.OrdinalIgnoreCase)) { // We were directed back to the redirect URI. Extract the code from the query string Dictionary <string, string> queryParametes = args.Uri.GetQueryParameters(); authenticationResult = new AuthenticationResult() { Code = queryParametes["code"] }; // All done. Close the window. authWindow.Close(); } }; authWindow.Closed += (sender, args) => { if (authenticationResult == null) { AuthComplete.Set(); return; } Task.Factory.StartNew(async() => { TokenResponse currentToken = await OneDriveClient.GetAccessToken(authenticationResult).ConfigureAwait(false); currentToken.SaveProtectedToken(tokenPath); TokenSuccess = true; AuthComplete.Set(); }); }; authWindow.Loaded += (sender, args) => { authWindow.Browser.Navigate(logoutUri); SyncPro.UI.NativeMethods.User32.SetForegroundWindow(new WindowInteropHelper(authWindow).Handle); }; authWindow.ShowDialog(); AuthComplete.WaitOne(); }