Exemple #1
0
        public static bool VerifyCredentialsAndDetectAuthScheme(IBlogCredentials blogCredentials, SharePointClient client)
        {
            //Attempt to execute the GetUsersBlogs() operation using standard HTTP authentication
            //If the server challenges with an HTTP 401, but doesn't include with WWW-authentication
            //header, then the server is configured to use MetaWeblog for authentication, so we
            //re-issue the request using that authentication scheme instead.

            AuthenticationScheme authScheme         = AuthenticationScheme.Http;
            AuthenticationScheme requiredAuthScheme = AuthenticationScheme.Unknown;

            while (requiredAuthScheme == AuthenticationScheme.Unknown || authScheme != requiredAuthScheme)
            {
                if (requiredAuthScheme != AuthenticationScheme.Unknown)
                {
                    authScheme = requiredAuthScheme;
                }

                blogCredentials.SetCustomValue(AUTH_SCHEME, authScheme.ToString());
                try
                {
                    TransientCredentials tc = new TransientCredentials(blogCredentials.Username, blogCredentials.Password, null);
                    client.Credentials.TransientCredentials = tc;
                    client.InitTransientCredential(tc);
                    client.GetUsersBlogs();
                    return(true);
                }
                catch (WebException e)
                {
                    requiredAuthScheme = GetRequiredAuthScheme(e);
                }
                catch (BlogClientHttpErrorException e)
                {
                    requiredAuthScheme = GetRequiredAuthScheme(e.Exception);
                }
                catch (BlogClientAuthenticationException e)
                {
                    requiredAuthScheme = GetRequiredAuthScheme(e.WebException);
                }
                catch (Exception)
                {
                    throw;
                }
                Debug.Assert(requiredAuthScheme != AuthenticationScheme.Unknown, "Unexpected authscheme");                 //this would cause an infinite loop!
            }
            return(false);
        }
 protected override void Save(IPropertyBag propertyBag)
 {
     propertyBag.WriteAdapterCustomProperty(nameof(EnableChunkedEncoding), EnableChunkedEncoding);
     propertyBag.WriteAdapterCustomProperty(nameof(RequestTimeout), (int)RequestTimeout.TotalSeconds);
     propertyBag.WriteAdapterCustomProperty(nameof(MaxRedirects), MaxRedirects);
     propertyBag.WriteAdapterCustomProperty(nameof(ContentType), ContentType);
     propertyBag.WriteAdapterCustomProperty(nameof(UseHandlerProxySettings), UseHandlerProxySettings);
     if (!UseHandlerProxySettings)
     {
         propertyBag.WriteAdapterCustomProperty(nameof(UseProxy), UseProxy);
         propertyBag.WriteAdapterCustomProperty(nameof(ProxyName), ProxyName);
         propertyBag.WriteAdapterCustomProperty(nameof(ProxyPort), ProxyPort);
         propertyBag.WriteAdapterCustomProperty("ProxyUsername", ProxyUserName);
         propertyBag.WriteAdapterCustomProperty(nameof(ProxyPassword), ProxyPassword);
     }
     propertyBag.WriteAdapterCustomProperty(nameof(AuthenticationScheme), AuthenticationScheme.ToString());
     propertyBag.WriteAdapterCustomProperty("Username", UserName);
     propertyBag.WriteAdapterCustomProperty(nameof(Password), Password);
     propertyBag.WriteAdapterCustomProperty(nameof(UseSSO), UseSSO);
     propertyBag.WriteAdapterCustomProperty(nameof(AffiliateApplicationName), AffiliateApplicationName);
     propertyBag.WriteAdapterCustomProperty(nameof(Certificate), Certificate);
 }
 protected override void Save(IPropertyBag propertyBag)
 {
     propertyBag.WriteAdapterCustomProperty("EnableChunkedEncoding", EnableChunkedEncoding);
     propertyBag.WriteAdapterCustomProperty("RequestTimeout", (int)RequestTimeout.TotalSeconds);
     propertyBag.WriteAdapterCustomProperty("MaxRedirects", MaxRedirects);
     propertyBag.WriteAdapterCustomProperty("ContentType", ContentType);
     propertyBag.WriteAdapterCustomProperty("UseHandlerProxySettings", UseHandlerProxySettings);
     if (!UseHandlerProxySettings)
     {
         propertyBag.WriteAdapterCustomProperty("UseProxy", UseProxy);
         propertyBag.WriteAdapterCustomProperty("ProxyName", ProxyName);
         propertyBag.WriteAdapterCustomProperty("ProxyPort", ProxyPort);
         propertyBag.WriteAdapterCustomProperty("ProxyUsername", ProxyUserName);
         propertyBag.WriteAdapterCustomProperty("ProxyPassword", ProxyPassword);
     }
     propertyBag.WriteAdapterCustomProperty("AuthenticationScheme", AuthenticationScheme.ToString());
     propertyBag.WriteAdapterCustomProperty("Username", UserName);
     propertyBag.WriteAdapterCustomProperty("Password", Password);
     propertyBag.WriteAdapterCustomProperty("UseSSO", UseSSO);
     propertyBag.WriteAdapterCustomProperty("AffiliateApplicationName", AffiliateApplicationName);
     propertyBag.WriteAdapterCustomProperty("Certificate", Certificate);
 }
 /// <summary>
 /// Sets scheme to the built authentication header value with the provided AuthenticationScheme enumeration.
 /// </summary>
 /// <param name="scheme">Enumeration with default authentication header schemes.</param>
 /// <returns>Authentication header value parameter builder.</returns>
 public IAuthenticationHeaderValueParameterBuilder WithScheme(AuthenticationScheme scheme)
 {
     this.authenticatedHeaderValue.Scheme = scheme.ToString();
     return(this);
 }
 /// <summary>
 /// Sets scheme to the built authentication header value with the provided AuthenticationScheme enumeration.
 /// </summary>
 /// <param name="scheme">Enumeration with default authentication header schemes.</param>
 /// <returns>Authentication header value parameter builder.</returns>
 public IAuthenticationHeaderValueParameterBuilder WithScheme(AuthenticationScheme scheme)
 {
     this.authenticatedHeaderValue.Scheme = scheme.ToString();
     return this;
 }
Exemple #6
0
 /// <summary>
 /// Tests whether an unauthorized result contains authentication header with the provided default scheme.
 /// </summary>
 /// <param name="scheme">Enumeration containing default schemes.</param>
 /// <returns>Unauthorized result test builder with AndAlso() method.</returns>
 public IAndUnauthorizedTestBuilder ContainingAuthenticationHeaderChallenge(AuthenticationScheme scheme)
 {
     return(this.ContainingAuthenticationHeaderChallenge(scheme.ToString()));
 }
        private HttpClient CreateHttpClient(AuthenticationScheme authScheme = AuthenticationScheme.None, string token = "", ContentType contentType = ContentType.Json)
        {
            HttpClient httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(GetEnumDescription(contentType)));
            if (this.headers.Count > 0)
            {
                foreach (var header in headers)
                {
                    httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                }
            }



            if (!string.IsNullOrEmpty(token) && (uint)authScheme > 0U)
            {
                this._logger.LogDebug("Authorization: {0}", (object)(httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authScheme.ToString("G"), token)));
            }
            return(httpClient);
        }