protected void InitializeAuthenticationResponse(AuthenticationResponse response)
 {
     response.AccessToken.YammerContext = this;
     response.Network.YammerContext = this;
     response.User.YammerContext = this;
 }
        public void EnsureAuthenticated()
        {
            // is the authentication toek empty or in an error state?
            if (String.IsNullOrEmpty(this.AuthToken) || this.AuthTokenIsError)
            {
                String code = this.Context.Request["code"];

                if (code == null)
                {
                    String url = this.Context.Request.Url.ToString();

                    int lastCode = url.LastIndexOf("?code=");
                    if (lastCode > 0)
                    {
                        code = url.Substring(lastCode + 6);
                    }
                }
                // have we received an initial authenticate code from Yammer?  If so, validate it and use it.
                if (code != null)
                {
                    this.AccessCode = code as String;

                    // prepare to call Yammer to get a valid authentication token given our code & secret.
                    String url = String.Format(YammerOAuthValidatorUrl, this.ClientId, this.ClientSecret, this.AccessCode);

                    HttpWebRequest hwr = WebRequest.CreateHttp(url);

                    WebResponse wr = hwr.GetResponse();

                    using (Stream s = wr.GetResponseStream())
                    {
                        // deserialize the result.
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AuthenticationResponse), this.Settings);

                        this.authenticationResponse = serializer.ReadObject(s) as AuthenticationResponse;

                        this.InitializeAuthenticationResponse(this.authenticationResponse);

                        // if we have a valid result, so keep going.
                        if (this.authenticationResponse != null)
                        {
                            this.AuthTokenIsError = false;

                            // let's persist this in session state.
                            this.AuthenticationResponseSessionStorage = this.AuthenticationResponseAsString;
                            this.AuthToken = this.AuthenticationResponse.AccessToken.Token;

                            if (this.Context.User != null && this.PersistTokensInDatabase)
                            {
                                this.SaveAuthenticationResponseToken(this.Context.User.Identity.Name);
                            }
                        }
                    }
                }
                else
                {
                    // we don't seem to have an active token
                    if (this.Context.User != null && !this.AuthTokenIsError)
                    {
                        // can we load a token from the database for this user?
                        if (!this.ForceRefreshToken && this.LoadAuthenticationResponseToken(this.Context.User.Identity.Name) && this.PersistTokensInDatabase)
                        {
                            this.AuthenticationResponseSessionStorage = this.AuthenticationResponseAsString;
                            this.AuthToken = this.AuthenticationResponse.AccessToken.Token;

                            // great, we loaded a token, let's return.
                            return;
                        }
                    }

                    // no token available to us, so start the redirection process over to Yammer to kick off the OAuth flow.
                    String url = String.Format(YammerOAuthUrl, this.ClientId, this.Context.Request.Url);

                    this.Context.Response.Redirect(url);
                }
            }
        }
 protected void InitializeAuthenticationResponse(AuthenticationResponse response)
 {
     response.AccessToken.YammerContext = this;
     response.Network.YammerContext     = this;
     response.User.YammerContext        = this;
 }