A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication/local-development-dev-accounts below:

Authenticate .NET apps to Azure using developer accounts - .NET

Authenticate .NET apps to Azure services during local development using developer accounts

During local development, applications need to authenticate to Azure to access various Azure services. Two common approaches for local authentication are to use a service principal or to use a developer account. This article explains how to use a developer account. In the sections ahead, you learn:

For an app to authenticate to Azure during local development using the developer's Azure credentials, the developer must be signed-in to Azure from one of the following developer tools:

The Azure Identity library can detect that the developer is signed-in from one of these tools. The library can then obtain the Microsoft Entra access token via the tool to authenticate the app to Azure as the signed-in user.

This approach takes advantage of the developer's existing Azure accounts to streamline the authentication process. However, a developer's account likely has more permissions than required by the app, therefore exceeding the permissions the app runs with in production. As an alternative, you can create application service principals to use during local development, which can be scoped to have only the access needed by the app.

Create a Microsoft Entra group for local development

Create a Microsoft Entra group to encapsulate the roles (permissions) the app needs in local development rather than assigning the roles to individual service principal objects. This approach offers the following advantages:

  1. Navigate to the Microsoft Entra ID overview page in the Azure portal.

  2. Select All groups from the left-hand menu.

  3. On the Groups page, select New group.

  4. On the New group page, fill out the following form fields:

  5. Select the No members selected link under Members to add members to the group.

  6. In the flyout panel that opens, search for the service principal you created earlier and select it from the filtered results. Choose the Select button at the bottom of the panel to confirm your selection.

  7. Select Create at the bottom of the New group page to create the group and return to the All groups page. If you don't see the new group listed, wait a moment and refresh the page.

  1. Use the az ad group create command to create groups in Microsoft Entra ID.

    az ad group create \
        --display-name <group-name> \
        --mail-nickname <group-mail-nickname> \
        --description <group-description>
    

    The --display-name and --mail-nickname parameters are required. The name given to the group should be based on the name and environment of the app to indicate the group's purpose.

  2. To add members to the group, you need the object ID of the application service principal, which is different than the application ID. Use the az ad sp list command to list the available service principals:

    az ad sp list \
        --filter "startswith(displayName, '<group-name>')" \
        --query "[].{objectId:id, displayName:displayName}"
    

    The --filter parameter accepts OData-style filters and can be used to filter the list as shown. The --query parameter limits the output to only the columns of interest.

  3. Use the az ad group member add command to add members to the group:

    az ad group member add \
        --group <group-name> \
        --member-id <object-id>
    
Assign roles to the group

Next, determine what roles (permissions) your app needs on what resources and assign those roles to the Microsoft Entra group you created. Groups can be assigned a role at the resource, resource group, or subscription scope. This example shows how to assign roles at the resource group scope, since most apps group all their Azure resources into a single resource group.

  1. In the Azure portal, navigate to the Overview page of the resource group that contains your app.

  2. Select Access control (IAM) from the left navigation.

  3. On the Access control (IAM) page, select + Add and then choose Add role assignment from the drop-down menu. The Add role assignment page provides several tabs to configure and assign roles.

  4. On the Role tab, use the search box to locate the role you want to assign. Select the role, and then choose Next.

  5. On the Members tab:

  6. On the Review + assign tab, select Review + assign at the bottom of the page.

  1. Use the az role definition list command to get the names of the roles that a Microsoft Entra group or service principal can be assigned to:

    az role definition list \
        --query "sort_by([].{roleName:roleName, description:description}, &roleName)" \
        --output table
    
  2. Use the az role assignment create command to assign a role to the Microsoft Entra group you created:

    az role assignment create \
        --assignee "<group-object-Id>" \
        --role "<role-name>" \
        --resource-group "<resource-group-name>"
    

    For information on assigning permissions at the resource or subscription level using the Azure CLI, see Assign Azure roles using the Azure CLI.

Next, sign-in to Azure using one of several developer tools that can be used to perform authentication in your development environment. The account you authenticate should also exist in the Microsoft Entra group you created and configured earlier.

Developers using Visual Studio 2017 or later can authenticate using their developer account through the IDE. Apps using DefaultAzureCredential or VisualStudioCredential can discover and use this account to authenticate app requests when running locally. This account is also used when you publish apps directly from Visual Studio to Azure.

  1. Inside Visual Studio, navigate to Tools > Options to open the options dialog.

  2. In the Search Options box at the top, type Azure to filter the available options.

  3. Under Azure Service Authentication, choose Account Selection.

  4. Select the drop-down menu under Choose an account and choose to add a Microsoft account.

  5. In the window that opens, enter the credentials for your desired Azure account, and then confirm your inputs.

  6. Select OK to close the options dialog.

Developers coding outside of an IDE can also use the Azure CLI to authenticate. Apps using DefaultAzureCredential or AzureCliCredential can then use this account to authenticate app requests when running locally.

To authenticate with the Azure CLI, run the az login command. On a system with a default web browser, the Azure CLI launches the browser to authenticate the user.

az login

For systems without a default web browser, the az login command uses the device code authentication flow. The user can also force the Azure CLI to use the device code flow rather than launching a browser by specifying the --use-device-code argument.

az login --use-device-code

Developers coding outside of an IDE can also use the Azure Developer CLI to authenticate. Apps using DefaultAzureCredential or AzureDeveloperCliCredential can then use this account to authenticate app requests when running locally.

To authenticate with the Azure Developer CLI, run the azd auth login command. On a system with a default web browser, the Azure Developer CLI launches the browser to authenticate the user.

azd auth login

For systems without a default web browser, the azd auth login --use-device-code uses the device code authentication flow. The user can also force the the Azure Developer CLI to use the device code flow rather than launching a browser by specifying the --use-device-code argument.

azd auth login --use-device-code

Developers coding outside of an IDE can also use Azure PowerShell to authenticate. Apps using DefaultAzureCredential or AzurePowerShellCredential can then use this account to authenticate app requests when running locally.

To authenticate with Azure PowerShell, run the command Connect-AzAccount. On a system with a default web browser and version 5.0.0 or later of Azure PowerShell, it launches the browser to authenticate the user.

Connect-AzAccount

For systems without a default web browser, the Connect-AzAccount command uses the device code authentication flow. The user can also force Azure PowerShell to use the device code flow rather than launching a browser by specifying the UseDeviceAuthentication argument.

Connect-AzAccount -UseDeviceAuthentication
Authenticate to Azure services from your app

The Azure Identity library provides various credentials—implementations of TokenCredential adapted to supporting different scenarios and Microsoft Entra authentication flows. The steps ahead demonstrate how to use DefaultAzureCredential when working with user accounts locally.

Implement the code

DefaultAzureCredential is an opinionated, ordered sequence of mechanisms for authenticating to Microsoft Entra ID. Each authentication mechanism is a class derived from the TokenCredential class and is known as a credential. At runtime, DefaultAzureCredential attempts to authenticate using the first credential. If that credential fails to acquire an access token, the next credential in the sequence is attempted, and so on, until an access token is successfully obtained. In this way, your app can use different credentials in different environments without writing environment-specific code.

To use DefaultAzureCredential, add the Azure.Identity and optionally the Microsoft.Extensions.Azure packages to your application:

In a terminal of your choice, navigate to the application project directory and run the following commands:

dotnet add package Azure.Identity
dotnet add package Microsoft.Extensions.Azure

Right-click your project in Visual Studio's Solution Explorer window and select Manage NuGet Packages. Search for Azure.Identity, and install the matching package. Repeat this process for the Microsoft.Extensions.Azure package.

Azure services are accessed using specialized client classes from the various Azure SDK client libraries. These classes and your own custom services should be registered so they can be accessed via dependency injection throughout your app. In Program.cs, complete the following steps to register a client class and DefaultAzureCredential:

  1. Include the Azure.Identity and Microsoft.Extensions.Azure namespaces via using directives.
  2. Register the Azure service client using the corresponding Add-prefixed extension method.
  3. Pass an instance of DefaultAzureCredential to the UseCredential method.
builder.Services.AddAzureClients(clientBuilder =>
{
    clientBuilder.AddBlobServiceClient(
        new Uri("https://<account-name>.blob.core.windows.net"));

    clientBuilder.UseCredential(new DefaultAzureCredential());
});

An alternative to the UseCredential method is to provide the credential to the service client directly:

builder.Services.AddSingleton<BlobServiceClient>(_ =>
    new BlobServiceClient(
        new Uri("https://<account-name>.blob.core.windows.net"),
        new DefaultAzureCredential()));

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4