Search code examples
c#.netsvnsharpsvn

NullReferenceException in SharpSvn's SvnClient when the supplied DefaultCredentials returns null


I'm using SharpSvn in my project to communicate with SVN. For some SVN repostories, I already know the user name and password, while for others, I don't. Thus, I set the SvnClient.Authentication.DefaultCredentials property to an instance of my own implementation of System.Net.ICredentials (pseudo code):

public class MyCredentials : ICredentials
{
    public NetworkCredential GetCredential(Uri uri, string authType)
    {
        if (AreCredentialsKnown(uri))
            return new NetworkCredential("KnownUserName", "KnownPassword");
        else
            return null;
    }
}

This works fine for known credentials. However, if the credentials are not known, a NullReferenceException occurs in SharpSvn:

Object reference not set to an instance of an object.
   at System.Net.NetworkCredential.InternalGetUserName()
   at System.Net.NetworkCredential.get_UserName()
   at SharpSvn.Implementation.SvnCredentialWrapper.OnUserNamePassword(Object sender, SvnUserNamePasswordEventArgs e) in f:\qqn\sharpsvn-dist-1.6\src\sharpsvn\svnauthentication.h:line 619

This is apparently caused by the fact that I return null from the GetCredential method. However, as is clearly stated by the MSDN documentation on the ICredentials interface:

The GetCredential method returns a NetworkCredential instance that contains the credentials that are associated with the specified URI and authorization scheme. When no credentials are available, the GetCredential method returns null.

Is this a bug in SharpSvn? Otherwise, what am I supposed to return when the credentials are not known?

Edit: I just discovered that I can return new NetworkCredentials() instead of null to indicate to SharpSvn that the credentials are unknown. Still, I think this is a bug in SharpSvn as returning null should work equally well.


Solution

  • The .DefaultCredentials is only an alternative for providing one explicit username and password to the Subversion libraries.

    If you need a more advanced prompting mechanism you should hook the .UserNamePassword event on SvnClient.Autorization. This also allows managing if passwords should be saved, etc.

    There is a default implementation in SharpSvn.UI.dll which hooks some standard dialogs.