Search code examples
c#tortoisesvnsvn-client

C# SvnClient Get Authorization Username


I can't seem to find a way to get the username through C# SvnClient.

In command line, you have ability to type "svn auth" in command prompt, and it shows the username there.

However, it is not as simple in the C# SvnClient API.

The API does have an "Authorization" Property -- which, unfortunately does not have a direct function call to grab the username.

Anyone know how?


Solution

  • After long solo search, I found it is possible to do. -- Though it is not pretty

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

    Unfortunately, this does not return back any public fields for username or password. This does however, have a private field called "_filename" that can be used to get the username / password. --- Which can be accessed by using reflection.

    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();
            }
        }
    }