public static void AdalIdTest()
        {
            IHttpWebRequest request = NetworkPlugin.HttpWebRequestFactory.Create("https://test");
            AdalIdHelper.AddAsHeaders(request);

            Verify.AreEqual(4, request.Headers.Count);
            Verify.IsNotNull(request.Headers[AdalIdParameter.Product]);
            Verify.IsNotNull(request.Headers[AdalIdParameter.Version]);
            Verify.IsNotNull(request.Headers[AdalIdParameter.CpuPlatform]);
#if TEST_ADAL_WINRT_UNIT
            Verify.IsNull(request.Headers[AdalIdParameter.OS]);
            Verify.IsNotNull(request.Headers[AdalIdParameter.DeviceModel]);
#else
            Verify.IsNotNull(request.Headers[AdalIdParameter.OS]);
            Verify.IsNull(request.Headers[AdalIdParameter.DeviceModel]);
#endif

            RequestParameters parameters = new RequestParameters(null, new ClientKey("client_id"));
            AdalIdHelper.AddAsQueryParameters(parameters);

            Verify.AreEqual(5, parameters.Count);
            Verify.IsNotNull(parameters[AdalIdParameter.Product]);
            Verify.IsNotNull(parameters[AdalIdParameter.Version]);
            Verify.IsNotNull(parameters[AdalIdParameter.CpuPlatform]);
#if TEST_ADAL_WINRT_UNIT
            Verify.IsFalse(parameters.ContainsKey(AdalIdParameter.OS));
            Verify.IsNotNull(parameters[AdalIdParameter.DeviceModel]);
#else
            Verify.IsNotNull(parameters[AdalIdParameter.OS]);
            Verify.IsFalse(parameters.ContainsKey(AdalIdParameter.DeviceModel));
#endif
        }
        public void RequestParametersTest()
        {
            const string ClientId = "client_id";
            const string AdditionalParameter = "additional_parameter";
            const string AdditionalParameter2 = "additional_parameter2";
            string expectedString = string.Format("client_id=client_id&{0}={1}&{2}={3}", AdditionalParameter, EncodingHelper.UrlEncode(ComplexString), AdditionalParameter2, EncodingHelper.UrlEncode(ComplexString2));

            RequestParameters param = new RequestParameters(null, new ClientKey(ClientId));
            param[AdditionalParameter] = ComplexString;
            param[AdditionalParameter2] = ComplexString2;
            Verify.AreEqual(expectedString, param.ToString());

            param = new RequestParameters(null, new ClientKey(ClientId));
            param[AdditionalParameter] = ComplexString;
            param.AddSecureParameter(AdditionalParameter2, StringToSecureString(ComplexString2));
            Verify.AreEqual(expectedString, param.ToString());

            param = new RequestParameters(null, new ClientKey(ClientId));
            param.AddSecureParameter(AdditionalParameter, StringToSecureString(ComplexString));
            param.AddSecureParameter(AdditionalParameter2, StringToSecureString(ComplexString2));
            Verify.AreEqual(expectedString, param.ToString());

            param = new RequestParameters(new StringBuilder(expectedString));
            Verify.AreEqual(expectedString, param.ToString());
        }
        protected override void AddAditionalRequestParameters(RequestParameters requestParameters)
        {
            requestParameters[OAuthParameter.GrantType] = OAuthGrantType.JwtBearer;
            requestParameters[OAuthParameter.Assertion] = this.userAssertion.Assertion;
            requestParameters[OAuthParameter.RequestedTokenUse] = OAuthRequestedTokenUse.OnBehalfOf;

            // To request id_token in response
            requestParameters[OAuthParameter.Scope] = OAuthValue.ScopeOpenId;
        }
        protected override void AddAditionalRequestParameters(RequestParameters requestParameters)
        {
            if (this.userAssertion != null)
            {
                requestParameters[OAuthParameter.GrantType] = this.userAssertion.AssertionType;
                requestParameters[OAuthParameter.Assertion] = Convert.ToBase64String(Encoding.UTF8.GetBytes(this.userAssertion.Assertion));
            }
            else
            {
                requestParameters[OAuthParameter.GrantType] = OAuthGrantType.Password;
                requestParameters[OAuthParameter.Username] = this.userCredential.UserName;
#if ADAL_NET
                if (this.userCredential.SecurePassword != null)
                {
                    requestParameters.AddSecureParameter(OAuthParameter.Password, this.userCredential.SecurePassword);
                }
                else
                {
                    requestParameters[OAuthParameter.Password] = this.userCredential.Password;
                }
#endif
            }

            // To request id_token in response
            requestParameters[OAuthParameter.Scope] = OAuthValue.ScopeOpenId;
        }
        private async Task<AuthenticationResult> SendHttpMessageAsync(RequestParameters requestParameters)
        {
            string uri = HttpHelper.CheckForExtraQueryParameter(this.Authenticator.TokenUri);

            TokenResponse tokenResponse = await HttpHelper.SendPostRequestAndDeserializeJsonResponseAsync<TokenResponse>(uri, requestParameters, this.CallState);

            AuthenticationResult result = OAuth2Response.ParseTokenResponse(tokenResponse, this.CallState);

            if (result.RefreshToken == null && requestParameters.ContainsKey(OAuthParameter.RefreshToken))
            {
                result.RefreshToken = requestParameters[OAuthParameter.RefreshToken];
                Logger.Verbose(this.CallState, "Refresh token was missing from the token refresh response, so the refresh token in the request is returned instead");
            }

            result.IsMultipleResourceRefreshToken = (!string.IsNullOrWhiteSpace(result.RefreshToken) && !string.IsNullOrWhiteSpace(tokenResponse.Resource));
            return result;
        }
 protected async Task<AuthenticationResult> SendTokenRequestByRefreshTokenAsync(string refreshToken)
 {
     RequestParameters requestParameters = new RequestParameters(this.Resource, this.ClientKey);
     requestParameters[OAuthParameter.GrantType] = OAuthGrantType.RefreshToken;
     requestParameters[OAuthParameter.RefreshToken] = refreshToken;
     return await this.SendHttpMessageAsync(requestParameters);
 }
 protected virtual async Task<AuthenticationResult> SendTokenRequestAsync()
 {
     RequestParameters requestParameters = new RequestParameters(this.Resource, this.ClientKey);
     this.AddAditionalRequestParameters(requestParameters);
     return await this.SendHttpMessageAsync(requestParameters);
 }
 protected abstract void AddAditionalRequestParameters(RequestParameters requestParameters);
 public static void AddAsQueryParameters(RequestParameters parameters)
 {
     NetworkPlugin.RequestCreationHelper.AddAdalIdParameters(parameters);
 }
 protected override void AddAditionalRequestParameters(RequestParameters requestParameters)
 {            
 }
 protected override void AddAditionalRequestParameters(RequestParameters requestParameters)
 {
     requestParameters[OAuthParameter.GrantType] = OAuthGrantType.ClientCredentials;
 }
 protected override void AddAditionalRequestParameters(RequestParameters requestParameters)
 {
     requestParameters[OAuthParameter.GrantType] = OAuthGrantType.AuthorizationCode;
     requestParameters[OAuthParameter.Code] = this.authorizationCode;
     requestParameters[OAuthParameter.RedirectUri] = this.redirectUri.AbsoluteUri;
 }
        private RequestParameters CreateAuthorizationRequest(string loginHint, bool includeFormsAuthParam)
        {
            RequestParameters authorizationRequestParameters = new RequestParameters(this.Resource, this.ClientKey);
            authorizationRequestParameters[OAuthParameter.ResponseType] = OAuthResponseType.Code;

            authorizationRequestParameters[OAuthParameter.RedirectUri] = this.redirectUriRequestParameter;

            if (!string.IsNullOrWhiteSpace(loginHint))
            {
                authorizationRequestParameters[OAuthParameter.LoginHint] = loginHint;
            }

            if (this.CallState != null && this.CallState.CorrelationId != Guid.Empty)
            {
                authorizationRequestParameters[OAuthParameter.CorrelationId] = this.CallState.CorrelationId.ToString();
            }

            // ADFS currently ignores the parameter for now.
            if (promptBehavior == PromptBehavior.Always)
            {
                authorizationRequestParameters[OAuthParameter.Prompt] = PromptValue.Login;
            }
            else if (promptBehavior == PromptBehavior.RefreshSession)
            {
                authorizationRequestParameters[OAuthParameter.Prompt] = PromptValue.RefreshSession;
            }
            else if (promptBehavior == PromptBehavior.Never)
            {
                authorizationRequestParameters[OAuthParameter.Prompt] = PromptValue.AttemptNone;
            }

            if (includeFormsAuthParam)
            {
                authorizationRequestParameters[OAuthParameter.FormsAuth] = OAuthValue.FormsAuth;
            }

            AdalIdHelper.AddAsQueryParameters(authorizationRequestParameters);

            if (!string.IsNullOrWhiteSpace(extraQueryParameters))
            {
                // Checks for extraQueryParameters duplicating standard parameters
                Dictionary<string, string> kvps = EncodingHelper.ParseKeyValueList(extraQueryParameters, '&', false, this.CallState);
                foreach (KeyValuePair<string, string> kvp in kvps)
                {
                    if (authorizationRequestParameters.ContainsKey(kvp.Key))
                    {
                        throw new AdalException(AdalError.DuplicateQueryParameter, string.Format(AdalErrorMessage.DuplicateQueryParameterTemplate, kvp.Key));
                    }
                }

                authorizationRequestParameters.ExtraQueryParameter = extraQueryParameters;
            }

            return authorizationRequestParameters;
        }
 protected abstract void AddAditionalRequestParameters(RequestParameters requestParameters);
        private async Task<AuthenticationResult> SendHttpMessageAsync(RequestParameters requestParameters)
        {
            string uri = HttpHelper.CheckForExtraQueryParameter(this.Authenticator.TokenUri);

            TokenResponse tokenResponse = await HttpHelper.SendPostRequestAndDeserializeJsonResponseAsync<TokenResponse>(uri, requestParameters, this.CallState);

            return OAuth2Response.ParseTokenResponse(tokenResponse);
        }