/// <summary> /// Initializes new OAuthLogin instance. /// </summary> /// <param name="authSession">VKSession instance to authorize</param> /// <param name="securityScope">Security options to request for session</param> public OAuthLoginAssistant(VKSession authSession, VK.SecurityFlag securityScope) { if (authSession == null) throw new ArgumentNullException("authSession"); else if (securityScope == VK.SecurityFlag.None) throw new ArgumentException("Invalid security flags. Specify at least one."); this.AuthSession = authSession; this.SecurityScope = securityScope; OK_REDIRECT_URL = ((securityScope & VK.SecurityFlag.NoHTTPS) != 0 ? "http" : "https") + OK_REDIRECT_URL; }
private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e) { // Tracking web browser navigation to detect if user has logged in & session authorized string url = e.Url.AbsoluteUri; if (url.StartsWith(OK_REDIRECT_URL)) { // Parse query string and determine what happend int i = OK_REDIRECT_URL.Length + 1; string queryString = url.Substring(i, url.Length - i); var qs = HttpUtility.ParseQueryString(queryString, Encoding.UTF8); // Check for errors if (qs["error"] != null) { string error = qs["error"]; string reason = qs["error_reason"]; string descr = qs["error_description"]; // Trigger event this.LoginFailed(this, error, reason, descr); } else if (qs["access_token"] != null) { // Load received data string token = qs["access_token"]; string secret = qs["secret"]; int user_id = int.Parse(qs["user_id"]); // Token expiration long expires_in = 0; if ((SecurityScope & VK.SecurityFlag.UniversalToken) == VK.SecurityFlag.UniversalToken) { // If we have universal-use token, it will expire really not soon. expires_in = -1; // infinite } else { // If no universal usage requested, token will expire after some time. expires_in = long.Parse(qs["expires_in"]); } // Check server rejected NoHTTPS mode if (qs["https_required"] == "1") { // User disabled possibility to work without SSL (in privacy settings?) or NoHTTPS blocked by VK // NoHTTPS is not enabled for this session SecurityScope &= ~VK.SecurityFlag.NoHTTPS; } // Extract VK user login cookies from IE var loginVkCom = new Uri("https://login.vk.com/"); var vkCom = new Uri("https://vk.com/"); var vkExtractedCookies = new CookieContainer(); try { vkExtractedCookies.Add( Win32CookieHack.GetIECookies(vkCom) .GetCookies(vkCom) ); vkExtractedCookies.Add( Win32CookieHack.GetIECookies(loginVkCom) .GetCookies(loginVkCom) ); } catch (Exception ex) { Debug.WriteLine("Exception somewhere in \"VK9.Login.OAuth.Windows.Win32CookieHack\", failed to extract cookies from IE?"); Debug.WriteLine(ex); Debugger.Break(); } // Load cookies into collection this.UserLoginCookies = new CookieCollection(); this.UserLoginCookies.Add(cookies: vkExtractedCookies.GetCookies(loginVkCom)); this.UserLoginCookies.Add(cookies: vkExtractedCookies.GetCookies(vkCom)); // Activate session instance AuthSession.AssignToken(new VK.AuthToken(token, expires_in), this.SecurityScope, new VK.UID(user_id), secret); // Done this.LoginCompleted(this); } else throw new NotSupportedException("Unknown redirect URL in OAuth sequence. VK9 library is out of date...?"); this.WebLoginForm.Close(); } }
/// <summary> /// Disposes of any resources (including created form), associated with this instance. /// </summary> public void Dispose() { // Dispose web login form if (this.WebLoginForm != null) { this.WebLoginForm.Dispose(); this.WebLoginForm = null; } // Clear state this.AuthSession = null; this.SecurityScope = VK.SecurityFlag.None; this.LoginCompleted = null; this.LoginFailed = null; }