[Mono-bugs] [Bug 688007] WebClient.OpenRead() fails when trying to access an anonymous ftp site

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Thu Apr 28 07:18:25 EDT 2011


https://bugzilla.novell.com/show_bug.cgi?id=688007

https://bugzilla.novell.com/show_bug.cgi?id=688007#c8


--- Comment #8 from QuickJack . <test051102 at hotmail.com> 2011-04-28 11:18:24 UTC ---
Hi Gonzalo,

I have taken a deeper look into the issue and found a possible solution.

###############################
#Analysis
###############################
The current implementation of WebClient.SetupRequest() is as follows:

WebRequest SetupRequest (Uri uri)
{
    WebRequest request = GetWebRequest (uri);
    if (Proxy != null)
        request.Proxy = Proxy;
    if (credentials != null)
        request.Credentials = credentials;

    //additional code not included for this demo
}

It starts by setting up proxy servers and credentials. But it does not try to
extract credentials from the url.


###############################
#Solution
###############################
I have extended the above part as follows:

WebRequest SetupRequest (Uri uri)
{
    WebRequest request = GetWebRequest (uri);
    if (Proxy != null)
        request.Proxy = Proxy;
    if (credentials != null)
        request.Credentials = credentials;
    else if(uri.AbsoluteUri.Contains("@"))
    {
        //extract username/password
        request.Credentials = GetCredentials(uri.AbsoluteUri);
    }


    //additional (unchanged) code not included for this demo
}

internal NetworkCredential GetCredentials(string strUrl)
{
    int i = strUrl.IndexOf("//")+2;
    int j = strUrl.IndexOf('/', i);
    string strCredential=strUrl.Substring(i,j-i);
    strCredential = strCredential.Substring(0, strCredential.LastIndexOf('@'));
    string[] credentials=strCredential.Split(new char[]{':'});

    if (credentials.Length == 2)
    {
        if (credentials[0].Contains("\\"))
        {
            //extract domain name
            string[] names=credentials[0].Split(new char[]{'\\'});

            if (names.Length == 2)
                return new NetworkCredential(names[1], credentials[1],
names[0]);
        }

        return new NetworkCredential(credentials[0], credentials[1]);
    }

    return new NetworkCredential();
}

The GetCredentials() method extracts username/password and possible domain
names from the url and initializes a new NetworkCredential object with them.

I have done tests on a password protected ftp site in the intranet that runs on
IIS and requires username/password/domain name to authenticate as well as on my
Quick'n'Easy ftp test server that requires username/password only. All tests
were successful.

-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.


More information about the mono-bugs mailing list