从Visual Studio扩展

时间:2015-07-11 03:05:53

标签: c# visual-studio visual-studio-2015 visual-studio-extensions microsoft-account

我正在编写VS扩展,需要与服务器通信并识别用户,我想如果可能的话,因为这个扩展将是连接到服务器的唯一客户端,使用Visual Studio的内置支持微软帐户比实施我自己的帐户管理基础架构更有意义。

首先,由于Visual Studio开发人员可以使用各种有用的API,人们可能会认为获取当前用户的信息很容易。但是,实际上似乎没有任何明显的API可用于访问帐户;我检查了here并且没有列出任何相关服务(“配置文件”服务只允许您读取/写入为当前用户存储的设置)。

有没有人知道从Visual Studio扩展程序访问Microsoft帐户的(相对)简单方法?

修改

我尝试了Hadi Brais的建议,起初它似乎有效(我成功检索了信息);但是,每次Visual Studio都会在30年后崩溃。我注释掉了与注册表交互的行,并用变量的静态值替换它们,并且崩溃停止了。显然,访问Visual Studio的注册表项导致它崩溃。我甚至尝试过using语句和其他安全措施,但是似乎没有办法从扩展中安全地访问Visual Studio注册表项。那么有没有人知道任何官方API 可用于检索此信息而不会导致Visual Studio崩溃?

2 个答案:

答案 0 :(得分:2)

对于Visual Studio 2015(版本14.0),这是如何获取有关当前在Visual Studio中登录的用户的信息。您需要添加using Microsoft.Win32;

private static string GetUserEmailAddressVS14()
{
    // It's a good practice to request explicit permission from
    // the user that you want to use his email address and any
    // other information. This enables the user to be in control
    // of his/her privacy.

    // Assuming permission is granted, we obtain the email address.

    const string SubKey = "Software\\Microsoft\\VSCommon\\ConnectedUser\\IdeUser\\Cache";
    const string EmailAddressKeyName = "EmailAddress";
    const string UserNameKeyName = "DisplayName";

    RegistryKey root = Registry.CurrentUser;
    RegistryKey sk = root.OpenSubKey(SubKey);
    if (sk == null)
    {
        // The user is currently not signed in.
        return null;
    }
    else
    {
        // Get user email address.
        return (string)sk.GetValue(EmailAddressKeyName);

        // You can also get user name like this.
        // return (string)sk.GetValue(UserNameKeyName);
    }
}

答案 1 :(得分:1)

现在有IdeUser键的多个版本。我已经以这种方式从其他答案中重新实现了算法:

public static string GetUserEmailAddressFromVisualStudioRegistry()
{
    try
    {
        const string ConnectedUserSubKey = @"Software\Microsoft\VSCommon\ConnectedUser";
        const string EmailAddressKeyName = "EmailAddress";

        RegistryKey connectedUserSubKey = Registry.CurrentUser.OpenSubKey( ConnectedUserSubKey );

        string[] subKeyNames = connectedUserSubKey?.GetSubKeyNames();

        if ( subKeyNames == null || subKeyNames.Length == 0 )
        {
            return null;
        }

        int[] subKeysOrder = new int[subKeyNames.Length];

        for ( int i = 0; i < subKeyNames.Length; i++ )
        {
            Match match = Regex.Match( subKeyNames[i], @"^IdeUser(?:V(?<version>\d+))?$" );

            if ( !match.Success )
            {
                subKeysOrder[i] = -1;
                continue;
            }

            string versionString = match.Groups["version"]?.Value;

            if ( string.IsNullOrEmpty( versionString ) )
            {
                subKeysOrder[i] = 0;
            }
            else if ( !int.TryParse( versionString, out subKeysOrder[i] ) )
            {
                subKeysOrder[i] = -1;
            }
        }

        Array.Sort( subKeysOrder, subKeyNames );

        for ( int i = subKeyNames.Length - 1; i >= 0; i++ )
        {
            string cacheSubKeyName = $@"{subKeyNames[i]}\Cache";
            RegistryKey cacheKey = connectedUserSubKey.OpenSubKey( cacheSubKeyName );
            string emailAddress = cacheKey?.GetValue( EmailAddressKeyName ) as string;

            if ( !string.IsNullOrWhiteSpace( emailAddress ) )
            {
                return emailAddress;
            }
        }
    }
    catch
    {
        // Handle exceptions here if it's wanted.
    }

    return null;
}

此算法尝试使用最新版本的所有版本,然后尝试所有其他同级版本。如果失败,它将返回null。