【问题标题】:NullReferenceException in SharpSvn's SvnClient when the supplied DefaultCredentials returns null当提供的 DefaultCredentials 返回 null 时,SharpSvn 的 SvnClient 中的 NullReferenceException
【发布时间】:2011-04-29 10:24:07
【问题描述】:

我在我的项目中使用 SharpSvn 与 SVN 进行通信。对于某些 SVN 存储库,我已经知道用户名和密码,而对于其他存储库,我不知道。因此,我将SvnClient.Authentication.DefaultCredentials 属性设置为我自己的System.Net.ICredentials 实现实例(伪代码):

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

这适用于已知凭据。但是,如果凭据未知,则会在 SharpSvn 中出现 NullReferenceException

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

这显然是由于我从 GetCredential 方法返回 null 造成的。但是,正如the MSDN documentation on the ICredentials interface 明确指出的那样:

GetCredential 方法返回一个 NetworkCredential 实例,其中包含与指定 URI 和授权方案相关联的凭据。当没有可用的凭据时,GetCredential 方法返回 null

这是 SharpSvn 中的错误吗?否则,当凭据未知时我应该返回什么?

编辑:我刚刚发现我可以返回 new NetworkCredentials() 而不是 null 以向 SharpSvn 表明凭据未知。不过,我认为这是 SharpSvn 中的一个错误,因为返回 null 应该同样有效。

【问题讨论】:

    标签: c# .net svn sharpsvn


    【解决方案1】:

    .DefaultCredentials 只是为 Subversion 库提供一个明确的用户名和密码的一种替代方法。

    如果您需要更高级的提示机制,您应该在 SvnClient.Autorization 上挂钩 .UserNamePassword 事件。这也允许管理是否应保存密码等。

    SharpSvn.UI.dll 中有一个默认实现,它挂钩了一些标准对话框。

    【讨论】:

      猜你喜欢
      • 2021-09-20
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 2023-03-17
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      相关资源
      最近更新 更多