从 Visual Studio 扩展访问当前 Microsoft 帐户 [英] Accessing current Microsoft account from Visual Studio extension

查看:24
本文介绍了从 Visual Studio 扩展访问当前 Microsoft 帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a VS extension which will need to communicate with a server and identify the user, and I figured that if possible, since this extension will be the only client connecting to the server, using Visual Studio's built-in support for Microsoft accounts would make more sense than implementing my own account management infrastructure.

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

At first, because of the variety of useful APIs available to a Visual Studio developer, one might think that getting info on the current user would be easy. However, there actually don't seem to be any obvious APIs that can be used to access accounts; I checked here and there weren't any related services listed (the "profile" services just allow you to read/write settings that are stored for the current user).

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

Does anyone know of a (relatively) simple way to access the Microsoft account from a Visual Studio extension?

编辑

我尝试了 Hadi Brais 的建议,起初它似乎有效(我成功检索到信息);但是,每次 Visual Studio 都会在大约 30 秒后崩溃.我注释掉了与注册表交互的行,并用变量的静态值替换了它们,崩溃就停止了.显然,访问 Visual Studio 的注册表项会导致它崩溃.我什至尝试了 using 语句和其他保护措施,但是似乎没有一种方法可以从扩展中安全地访问 Visual Studio 注册表项.那么有谁知道任何官方 API 可用于在不使 Visual Studio 崩溃的情况下检索此信息?

I tried Hadi Brais's suggestion, and at first it appeared to work (I successfully retrieved the information); however, each time, Visual Studio would crash about 30s afterward. I commented out the lines that interacted with the registry and replaced them with static values for the variables, and the crashes stopped. Clearly, accessing Visual Studio's registry keys was causing it to crash. I even tried using statements and other safeguards, however there doesn't seem to be a way to safely access the Visual Studio registry keys from an extension. So does anyone know of any official APIs that can be used to retrieve this information without crashing Visual Studio?

推荐答案

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

For Visual Studio 2015 (Version 14.0), this is how to get information about the user that is currently signed in in Visual Studio. You need to add 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);
    }
}

这篇关于从 Visual Studio 扩展访问当前 Microsoft 帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆