How can I retrieve RDP files for an Azure Virtual Desktop session using the Desktop Virtualization APIs in a C# .NET application?

DILIPK9603 1 Reputation point
2024-11-29T10:08:10.45+00:00

I'm trying to implement functionality in my C# .NET application that retrieves RDP files for an Azure Virtual Desktop session, similar to how the Remote Desktop client retrieves them. Specifically, I need to programmatically access RDP file details, such as the session host, username, and connection details, to generate RDP files dynamically for users.

I have already authenticated using Azure Identity, but I’m not sure how to use the Desktop Virtualization APIs to fetch the RDP file or its equivalent session details.

Could someone guide me on how to retrieve or generate the RDP file programmatically using the Azure Virtual Desktop APIs in .NET? Any examples or suggestions would be greatly appreciated. Desktop Virtualization REST API | Microsoft Learn

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,313 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,086 questions
Azure Virtual Desktop
Azure Virtual Desktop
A Microsoft desktop and app virtualization service that runs on Azure. Previously known as Windows Virtual Desktop.
1,666 questions
Remote Desktop
Remote Desktop
A Microsoft app that connects remotely to computers and to virtual apps and desktops.
4,697 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 28,376 Reputation points
    2025-01-18T18:24:21.0333333+00:00

    As perquisites you need ;

    • an app registered in Azure AD with the necessary API permissions for Azure Virtual Desktop. Grant the app the required permissions like Azure Virtual Desktop Reader or higher.
    • Familiarize yourself with the Azure Virtual Desktop API documentation on Microsoft Learn

    Then use the Azure.Identity library to authenticate against Azure. For instance, you can use DefaultAzureCredential if running in an environment with managed identities or service principals.

    var credential = new DefaultAzureCredential();
    

    Azure Virtual Desktop provides an API endpoint to fetch RDP session details for a user. You need to:

    1. Query the user session in a specific host pool.
    2. Generate the RDP file details.

    The REST API endpoint for fetching user sessions:

    GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DesktopVirtualization/hostPools/{hostPoolName}/sessions/{userSessionName}?api-version=2022-09-09-preview
    

    Here’s how you can use HttpClient to call the API:

    using System;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;
    
    public async Task<string> GetRdpFileAsync(string subscriptionId, string resourceGroupName, string hostPoolName, string userSessionName)
    {
        var baseUrl = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DesktopVirtualization/hostPools/{hostPoolName}/sessions/{userSessionName}?api-version=2022-09-09-preview";
    
        var credential = new DefaultAzureCredential();
        var token = await credential.GetTokenAsync(
            new Azure.Core.TokenRequestContext(new[] { "https://management.azure.com/.default" })
        );
    
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Token);
            var response = await client.GetAsync(baseUrl);
    
            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                // Parse the response and construct RDP file data
                return content;
            }
            else
            {
                throw new Exception($"Error retrieving RDP file: {response.ReasonPhrase}");
            }
        }
    }
    
    

    The API response will include details like the session host, user, and session metadata. Use these to construct the .rdp file content dynamically. An RDP file contains key-value pairs, such as:

    full address:s:<session-host>:3389
    username:s:<username>
    

    Simple function to generate an RDP file:

    public string GenerateRdpFile(string sessionHost, string username)
    {
        return $@"
    full address:s:{sessionHost}
    username:s:{username}
    prompt for credentials:i:1
    administrative session:i:1
    ";
    }
    

    Save the generated RDP file or return it to the user for download.

    public void SaveRdpFile(string rdpContent, string filePath)
    {
        System.IO.File.WriteAllText(filePath, rdpContent);
    }
    

    Links ti help you :

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.