【问题标题】:C# SvnClient Get Authorization UsernameC# SvnClient 获取授权用户名
【发布时间】:2018-06-07 02:22:04
【问题描述】:

我似乎找不到通过 C# SvnClient 获取用户名的方法。

在命令行中,您可以在命令提示符下键入“svn auth”,它会在那里显示用户名。

但是,在 C# SvnClient API 中它并不那么简单。

API 确实有一个“授权”属性——不幸的是,它没有直接的函数调用来获取用户名。

有人知道怎么做吗?

【问题讨论】:

  • 你的意思是来自 SharpSvn 库的 SvnClient 类吗?

标签: c# tortoisesvn svn-client


【解决方案1】:

经过长时间的单独搜索,我发现可以做到。 -- 虽然不好看

var cachedItems = SvnClient.Authentication.GetCachedProperties(SvnAuthenticationCacheType.UserNamePassword)

不幸的是,这不会返回用户名或密码的任何公共字段。但是,这确实有一个名为“_filename”的私有字段,可用于获取用户名/密码。 --- 可以通过反射访问。

foreach (var item in cachedItems)
{
    ///find the matching Uri to the repository you want
    if (item.RealmUri.AbsoluteUri == ...)
    {
        var type = item.GetType();
        var fields = type.GetFields();

        var filename = fields.First(x => x.Name == "_filename").GetValue(item);

        ///Now just need to parse the file
        using (var streamReader = new StreamReader(new FileStream(filename, FileMode.Open)))
        {
            while (streamReader.ReadLine() != "username") {}
            streamReader.ReadLine(); ///There is 1 garbage line after username before the actual username
            return streamReader.ReadLine();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 2022-01-23
    • 2014-10-04
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    相关资源
    最近更新 更多