使用多个用户帐户Work with multiple user accounts

如果有多个 Microsoft 帐户和/或单位或学校帐户,可将它们全部添加到 Visual Studio,以便可从任何帐户访问资源,而无需单独登录到这些帐户。If you have multiple Microsoft accounts and/or work or school accounts, you can add them all to Visual Studio so that you can access the resources from any account without having to sign in to it separately. Azure、Application Insights、Azure DevOps 和 Office 365 服务都支持简化的登录体验。Azure, Application Insights, Azure DevOps, and Office 365 services all support the streamlined sign-in experience.

将多个帐户添加到一台计算机上之后,如果你在另一台计算机上登录到 Visual Studio,则该组帐户会随你一起漫游。After you add multiple accounts on one machine, that set of accounts roams with you if you sign in to Visual Studio on another machine.

Note

尽管帐户名可漫游,但凭据却不能。Although the account names roam, the credentials do not. 第一次尝试在新的计算机上使用其资源时,将提示你输入这些其他帐户的凭据。You'll be prompted to enter credentials for those other accounts the first time you attempt to use their resources on a new machine.

本文介绍如何将多个帐户添加到 Visual Studio。This article shows you how to add multiple accounts to Visual Studio. 还显示如何查看可从“添加连接的服务”对话框、“服务器资源管理器”和“团队资源管理器”等位置的帐户访问的资源 。It also shows you how to see the resources accessible from those accounts in places such as the Add Connected Service dialog, Server Explorer, and Team Explorer.

登录 Visual StudioSign in to Visual Studio

使用 Microsoft 帐户或组织帐户登录到 Visual Studio。Sign into Visual Studio with a Microsoft account or an organizational account. 窗口上方会显示你的用户名,类似于:You should see your user name appear in the upper corner of the window, similar to this:

当前登录的用户

在服务器资源管理器中访问你的 Azure 帐户Access your Azure account in Server Explorer

若要打开服务器资源管理器,请选择“查看” > “服务器资源管理器” (或者,如果使用的是“常规”环境设置 ,请按 Ctrl+Alt+S) 。To open Server Explorer, choose View > Server Explorer (or, if you're using the "General" environment settings, press Ctrl+Alt+S). 展开“Azure”节点,注意它包含 Azure 帐户中可用的资源,该资源与用于登录 Visual Studio 的帐户相关联 。Expand the Azure node and notice that it contains the resources available in the Azure account that's associated with the account that you used to sign in to Visual Studio. 它看上去类似于下图:It looks similar to the following image:

展开了 Azure 节点的服务器资源管理器

在任何特定设备上首次使用 Visual Studio 时,对话框都将只显示在登录的账户下注册的订阅。The first time you use Visual Studio on any specific device, the dialog only shows the subscriptions registered under the account that you signed in with. 通过右键单击“Azure”节点、选择“管理和筛选订阅”并从帐户选取器控件添加帐户,可以直接从“服务器资源管理器”访问任何其他帐户的资源 。You can access resources for any of your other accounts directly from Server Explorer by right-clicking on the Azure node, choosing Manage and Filter Subscriptions, and then adding your accounts from the account picker control. 如果需要,可以通过单击向下箭头,从帐户列表中选择另一个帐户。You can then choose another account, if desired, by clicking the down arrow and choosing from the list of accounts. 选择帐户之后,可以自定义帐户下的哪些订阅在“服务器资源管理器”中显示 。After choosing the account, you can customize which subscriptions under that account to display in Server Explorer.

“管理 Azure 订阅”对话框

下次打开“服务器资源管理器”时,将显示所选订阅的资源 。The next time you open Server Explorer, the resources for that subscription are displayed.

通过“添加连接的服务”对话框,访问你的 Azure 帐户Access your Azure account via Add Connected Service dialog

  1. 打开一个现有项目,或创建一个新项目。Open an existing project, or create a new project.

  2. 选择“解决方案资源管理器”中的项目节点,然后右键单击并选择“添加” > “连接的服务” 。Choose the project node in Solution Explorer, and then right-click and choose Add > Connected Service.

    随即出现“添加连接的服务”向导,其中显示与 Visual Studio 个性化账户相关联的 Azure 帐户中的服务列表 。The Add Connected Service wizard appears and shows you the list of services in the Azure account that's associated with your Visual Studio personalization account. 无需单独登录 Azure。You don't have to sign in separately to Azure. 但是,第一次尝试从另一计算机访问其资源时,你需要登录到其他帐户。However, you do need to sign in to the other accounts the first time you attempt to access their resources from a different machine.

在 Web 项目中访问 Azure Active DirectoryAccess Azure Active Directory in a Web project

Azure Active Directory (AAD) 支持 ASP.NET MVC web 应用中的最终用户单一登录或 Web API 服务中的 AD 身份验证。Azure Active Directory (AAD) enables support for end-user single sign-in in ASP.NET MVC web apps or AD authentication in web API services. 域身份验证与单个用户帐户身份验证不同。Domain authentication is different from individual user account authentication. 有权访问 Active Directory 域的用户可以使用其现有的 AAD 帐户连接到 web 应用程序。Users that have access to your Active Directory domain can use their existing AAD accounts to connect to your web applications. Office 365 应用还可以使用域身份验证。Office 365 apps can also use domain authentication.

要了解此操作,请创建一个新的“ASP.NET Core Web 应用程序”项目 。To see this in action, create a new ASP.NET Core Web Application project. 在“新建 ASP.NET Core Web 应用程序”对话框中,选择“Web 应用程序”模板,然后选择“更改身份验证” 。In the New ASP.NET Core Web Application dialog box, choose the Web Application template, and then choose Change Authentication.

要了解此操作,请创建一个新的“ASP.NET Core Web 应用程序”项目 。To see this in action, create a new ASP.NET Core Web Application project. 在“创建新 ASP.NET Core Web 应用程序”页中,选择“Web 应用程序”模板,然后在“身份验证”下选择“更改” 。On the Create a new ASP.NET Core Web Application page, choose the Web Application template, and then choose Change under Authentication.

随即显示“更改身份验证”对话框,你可以在该对话框中选择要在应用程序中使用的身份验证类型 。The Change Authentication dialog box appears where you can choose what kind of authentication to use in your application.

适用于 ASP.NET 的更改身份验证对话框

有关 ASP.NET 中不同类型的身份验证的详细信息,请参阅在 Visual Studio 中创建 ASP.NET web 项目For more information about the different kinds of authentication in ASP.NET, see Create ASP.NET web projects in Visual Studio.

访问 Azure DevOps 组织Access your Azure DevOps organization

在主菜单中,选择“团队” > “管理连接”以打开“团队资源管理器 - 连接”窗口 。From the main menu, choose Team > Manage Connections to open the Team Explorer - Connect window. 选择“管理连接” > “连接到项目” 。Choose Manage Connections > Connect to a Project. 在“连接到项目”对话框中,从列表中选择一个项目(或选择“添加 TFS 服务器”并输入服务器的 URL) 。In the Connect to a Project dialog, select a project from the list (or select Add TFS Server and enter the URL to your server). 选择 URL 后,无需重新输入凭据即可登录。When you select a URL, you're logged in without having to reenter your credentials.

有关详细信息,请参阅连接到团队资源管理器中的项目For more information, see Connect to projects in Team Explorer.

向 Visual Studio 添加另一个帐户Add an additional account to Visual Studio

若要将其他帐户添加到 Visual Studio:To add an additional account to Visual Studio:

  1. 选择“文件” > “帐户设置” 。Choose File > Account Settings.

  2. 在“所有帐户”下,选择“添加帐户” 。Under All Accounts, choose Add an account.

  3. 在“登录到你的帐户”页面上,选择该帐户或选择“使用另一个帐户” 。On the Sign in to your account page, select the account or choose Use another account. 按照提示输入新的帐户凭据。Follow the prompts to enter the new account credentials.

(可选)现在可以转到“服务器资源管理器”,并查看与刚添加的帐户相关联的 Azure 服务 。(Optional) Now you can go to Server Explorer and see the Azure services associated with the account you just added. 在“服务器资源管理器”中,右键单击“Azure”节点并选择“管理和筛选订阅” 。In Server Explorer, right-click on the Azure node and choose Manage and Filter Subscriptions. 通过单击当前帐户旁的下拉箭头选择新的帐户,然后选择想要在“服务器资源管理器”中显示的订阅 。Choose the new account by clicking the drop-down arrow next to the current account, and then choose which subscriptions you want to display in Server Explorer. 应可以看到与指定订阅关联的所有服务。You should see all the services associated with the specified subscription. 即使当前未使用第二个帐户登录到 Visual Studio,也可登录到该帐户的服务和资源。Even though you're not currently signed into Visual Studio with the second account, you are signed in to that account's services and resources. “项目” > “添加连接的服务” 和“团队” > “连接到 Team Foundation Server” 也是如此。The same is true for Project > Add Connected Service and Team > Connect to Team Foundation Server.

使用设备代码流添加帐户Add an account using device code flow

在某些情况下,无法以常规方式登录或添加帐户。In some cases, you can't sign in or add an account in the regular manner. 如果 Internet Explorer 因某种原因被阻止,或网络设置了防火墙,则可能发生这种情况。This can happen if Internet Explorer is blocked for some reason, or if your network is behind a firewall. 要解决此问题,可以启用“设备代码流”来添加帐户或重新验证帐户 。To work around this, you can enable device code flow to add an account or reauthenticate your account. 借助设备代码流,可使用另一种浏览器或在另一个物理或虚拟 (VM) 机—上登录。Device code flow lets you sign in using a different browser or on a different machine—either physical or virtual (VM).

使用设备代码流登录:To sign in using device code flow:

  1. 打开“工具” > “选项” > “环境”下的 “帐户”页面,然后选择“在添加或重新验证帐户时启用设备代码流” 。Open the Accounts page under Tools > Options > Environment, and then select Enable device code flow when adding or re-authenticating an account. 选择“确定”关闭选项页 。Choose OK to close the options pages.

  2. 选择“文件” > “帐户设置”以打开帐户管理页面 。Choose File > Account Settings to open the account management page.

  3. 在“所有帐户”下,选择“添加帐户” 。Choose Add an account under All Accounts.

    会出现一个对话框,其中显示了要粘贴到 web 浏览器中的 URL 和代码。A dialog box shows you a URL and a code to paste into a web browser.

    设备代码流 URL 和代码

  4. 按“Ctrl+C”复制对话框文本,然后选择“确定”关闭对话框 。Press Ctrl+C to copy the text of the dialog, and then choose OK to close the dialog. 将复制的文本粘贴到文本编辑器(如记事本)中。Paste the text you copied into a text editor such as Notepad. 这使得在下一步中复制代码更加容易。This makes it easier to copy the code in the next step.

  5. 导航到机器或 web 浏览器上用于登录到 Visual Studio 的设备登录 URL,然后将复制的代码粘贴或输入到“代码”框中 。Navigate to the device login URL on the machine or web browser you want to use to sign in to Visual Studio, and then paste or enter the code you copied into the box that says Code.

    “Visual Studio”应用名称应会显示在页面的更下方 。The Visual Studio app name should appear further down on the page.

  6. 在“Visual Studio”下,选择“继续” 。Under Visual Studio, choose Continue.

    device-login-page.png

  7. 按照提示输入帐户凭据。Follow the prompts to enter your account credentials.

    随即出现一个页面,提示已在设备上登录 Visual Studio,可以关闭浏览器窗口。A page appears telling you that you've signed into Visual Studio on your device, and that you can close the browser window.

    通过浏览器登录 visual Studio 这一操作已完成

  8. 返回 Visual Studio 中的帐户管理页面,其中的“所有帐户”下列出了新添加的帐户 。Go back to the account management page in Visual Studio and you'll see the newly added account listed under All Accounts. 选择“关闭” 。Choose Close.

请参阅See also