示例#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);
        }
        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;
        }