コード例 #1
0
        /// <summary>
        /// Handle authentification and login in
        /// response to a component UI click.
        /// </summary>
        protected void Login(object sender, object e)
        {
            // If it's not valid, offer a login.
            if (!Auth.AuthCheck())
            {
                Dictionary <string, string> extra = new Dictionary <string, string>()
                {
                    { "response_type", "code" }
                };

                ClearToken();
                client.LoginAsync(extra).ContinueWith(t =>
                {
                    if (!t.Result.IsError)
                    {
                        Default.Token = t.Result.AccessToken;
                        log.Clear();
                        log.Add("Logged in to Axis at " + DateTime.Now.ToShortTimeString());
                        DateTime validTo = DateTime.Now.AddDays(2);
                        log.Add("Login valid to: " + validTo.ToLongDateString() + ", " + validTo.ToShortTimeString());
                        Message = "OK";
                        Axis.Properties.Settings.Default.LoggedIn = true;
                        Axis.Auth.RaiseEvent(true);

                        var doc = OnPingDocument();
                        if (doc != null)
                        {
                            doc.ScheduleSolution(10, LocalExpire);
                        }

                        void LocalExpire(GH_Document document) => ExpireSolution(false);
                    }
                    else
                    {
                        Debug.WriteLine("Error logging in: " + t.Result.Error);
                        log.Add(t.Result.ToString());
                        log.Add("Error logging in: " + t.Result.Error);
                        Axis.Properties.Settings.Default.LoggedIn = false;
                        Axis.Auth.RaiseEvent(false);

                        var doc = OnPingDocument();
                        if (doc != null)
                        {
                            doc.ScheduleSolution(10, LocalExpire);
                        }

                        void LocalExpire(GH_Document document) => ExpireSolution(false);
                    }
                    t.Dispose();
                });

                // Update our login time.
                Default.LastLoggedIn = DateTime.Now;
            }
            else
            {
                log.Add("Already logged in.");
                ExpireSolution(true);
            }

            if (Axis.Properties.Settings.Default.LoggedIn)
            {
                Message = "Logged In";
            }
            else
            {
                Message = "Error";
            }
        }