当提供的 DefaultCredentials 返回 null 时,SharpSvn 的 SvnClient 中出现 NullReferenceException

发布于 2024-11-04 02:19:31 字数 1565 浏览 2 评论 0原文

我在项目中使用 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 引起的。然而,正如 ICredentials 接口上的 MSDN 文档明确指出的< /a>:

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

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

编辑:我刚刚发现我可以返回new NetworkCredentials()而不是null来向SharpSvn表明凭据未知。尽管如此,我认为这是 SharpSvn 中的一个错误,因为返回 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮光沉寂 2024-11-11 02:19:31

.DefaultCredentials 只是向 Subversion 库提供一个显式用户名和密码的替代方法。

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

中有一个默认实现,它挂钩一些标准对话框。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文