示例#1
0
        public static string Prettify <T>(this MiiApiResponse <T> response)
        {
            string toReturn = "<div class='response'>";

            toReturn += RenderFact("Status", response.Status);
            toReturn += RenderFact("Error code", response.ErrorCode);
            toReturn += RenderFact("Error message", response.ErrorMessage);
            toReturn += RenderFact("Is a test user?", response.IsTestUser);

            if (response.Data == null)
            {
                toReturn += RenderFact("Data", null);
            }
            else if (typeof(IEnumerable).IsAssignableFrom(typeof(T)) && typeof(T) != typeof(string))
            {
                int ct = 0;

                foreach (var obj in response.Data as IEnumerable)
                {
                    toReturn += "<div class='fact'><h4>[" + ct.ToString() + "]</h4>";

                    Func <object, string> renderer = DEFAULT_RENDERER;
                    if (obj != null)
                    {
                        if (!RENDERERS.TryGetValue(obj.GetType(), out renderer))
                        {
                            renderer = DEFAULT_RENDERER;
                        }
                    }

                    toReturn += renderer(obj);

                    toReturn += "</div>";
                    ct++;
                }
            }
            else
            {
                Func <object, string> renderer = null;
                if (!RENDERERS.TryGetValue(typeof(T), out renderer))
                {
                    renderer = DEFAULT_RENDERER;
                }

                toReturn += renderer(response.Data);
            }

            toReturn += "</div>";

            return(toReturn);
        }
示例#2
0
        protected override void OnLoad(EventArgs e)
        {
            // If we've not yet been supplied a TokenManager then build a session-based one
            if (this.TokenManager == null)
            {
                // First try pulling key and secret information from application settings
                string consumerKey    = ConfigurationManager.AppSettings[MiiCard.ConfigSettingNameMiiCardConsumerKey];
                string consumerSecret = ConfigurationManager.AppSettings[MiiCard.ConfigSettingNameMiiCardConsumerSecret];

                // We require at least a consumer key be available - if it's not, bail out as there's no further
                // we can realistically go
                if (string.IsNullOrWhiteSpace(consumerKey))
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  "The TokenManager was not initialised with suitable consumer key and secret information, and the information could not " +
                                  "be found in web.config. Either explicitly specify an IConsumerTokenManager to be used via the TokenManager property, or " +
                                  "add appropriate entries to your web.config's appSettings section with key names {0} and {1}.",
                                  MiiCard.ConfigSettingNameMiiCardConsumerKey,
                                  MiiCard.ConfigSettingNameMiiCardConsumerSecret)
                              );
                }

                this.TokenManager = new SessionStateConsumerTokenManager(consumerKey, consumerSecret);
            }

            Page.RegisterRequiresViewStateEncryption();

            var consumer = this.GetConsumer();
            AuthorizedTokenResponse authTokenResponse = null;

            try
            {
                authTokenResponse = consumer.ProcessUserAuthorization();
            }
            catch (Exception ex)
            {
                this.AuthorisationFailed(this, new MiiCardAuthorisationFailureEventArgs(ex));
            }

            if (authTokenResponse != null)
            {
                // We've been successfully authenticated - if we've been configured to do so then pull down the
                // user's profile so that it can be made available to the event handler
                MiiApiResponse <MiiUserProfile> response = null;

                if (this.LoadUserProfileOnAuthorise)
                {
                    var service = new MiiCardOAuthClaimsService(this.TokenManager.ConsumerKey, this.TokenManager.ConsumerSecret, authTokenResponse.AccessToken, this.TokenManager.GetTokenSecret(authTokenResponse.AccessToken));
                    response = service.GetClaims();

                    if (response.Status == MiiApiCallStatus.Success)
                    {
                        // User profile will be stored in the correct location based on the setting of the
                        // this.UserProfileStorage property
                        this.UserProfile = response;
                        this.AuthorisationSucceeded(this, new MiiCardAuthorisationSuccessEventArgs(authTokenResponse.AccessToken, this.TokenManager.GetTokenSecret(authTokenResponse.AccessToken), authTokenResponse.ExtraData, response));
                    }
                    else
                    {
                        this.AuthorisationFailed(this, new MiiCardAuthorisationFailureEventArgs(response.ErrorCode, response.ErrorMessage));
                    }
                }
                else
                {
                    this.AuthorisationSucceeded(this, new MiiCardAuthorisationSuccessEventArgs(authTokenResponse.AccessToken, this.TokenManager.GetTokenSecret(authTokenResponse.AccessToken), authTokenResponse.ExtraData, null));
                }
            }

            base.OnLoad(e);
        }