public string GetResponseHtml(IDictionary <string, string> parameters, Uri signinUri)
        {
            string code = parameters["code"];

            // Exchange the Request Token for an Access Token
            string appId     = _settings.OkApplicationId;
            string appSecret = _settings.OkApplicationSecret;
            string appPublic = _settings.OkApplicationPublic;

            string scheme = parameters["SERVER_PORT_SECURE"] == "1" ? "https" : "http";

            var callbackUri = new UriBuilder(string.Format("{0}://{1}", scheme, parameters["HTTP_HOST"]))
            {
                Path  = parameters["URL"],
                Query = string.Format("context={0}", parameters["context"])
            };

            var     service     = new OkClient(appId, appSecret, appPublic);
            dynamic accessToken = service.GetAccessToken(code, callbackUri.ToString());

            dynamic token = accessToken.access_token;

            service.AuthenticateWith(token.ToString());

            // Claims
            dynamic user = service.Get("users.getCurrentUser");

            string acsNamespace = _settings.AcsNamespace;
            string wtRealm      = string.Format(WtRealm, acsNamespace);
            string wReply       = string.Format(WReply, acsNamespace);

            var requestMessage = new SignInRequestMessage(signinUri, wtRealm, wReply);

            // Add extracted claims
            var identity = new ClaimsIdentity(AuthenticationTypes.Federation);

            identity.AddClaim(new Claim(ClaimTypes.Name, user.name.ToString()));
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.uid.ToString()));
            identity.AddClaim(new Claim(OkClaims.OkToken, token.ToString()));

            var principal = new ClaimsPrincipal(identity);

            SignInResponseMessage responseMessage = FederatedPassiveSecurityTokenServiceOperations.ProcessSignInRequest(requestMessage, principal, this);

            responseMessage.Context = parameters["context"];

            return(responseMessage.WriteFormPost());
        }
        public TokenData Get(IpData data)
        {
            // Try to validate token
            _service.AuthenticateWith(data.Token);

            dynamic user;

            try
            {
                user = _service.Get("users.getCurrentUser");
            }
            catch (Exception e)
            {
                throw new BadGatewayException(e);
            }

            if (user.error_msg != null)
            {
                throw new BadRequestException(user.error_msg.ToString());
            }

            try
            {
                return(new TokenData
                {
                    IdentityProvider = ProviderType.Odnoklassniki,
                    Name = user.name.ToString(),
                    UserIdentifier = user.uid.ToString(),
                    Token = data.Token,
                    TokenSecret = data.TokenSecret
                });
            }
            catch (Exception e)
            {
                string message = string.Format("Unable to receive Odnoklassniki profile: {0}", e);
                throw new InternalServerErrorException(message);
            }
        }