Given subscription key - how to find associated user?

Martin Kallukalam 440 Reputation points
2024-06-25T00:03:13.43+00:00

How do I find the user associated with a subscription key?
I understand I can find Sub key associated with a user. But is there a way to find User associated with a specific sub key?

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,463 questions
0 comments No comments
{count} votes

Accepted answer
  1. SwathiDhanwada-MSFT 18,996 Reputation points Moderator
    2024-06-25T06:03:16.17+00:00

    @Martin Kallukalam Here is sample script where it checks all the apim instances within your subscription to identify the respective subscription Id and user who owns it. Kindly fill in the respective values where "xxx" is mentioned.

    
    try {
        "Logging in to Azure..."
        Connect-AzAccount  -Tenant "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx" 
    }
    catch {
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
    
        
    $subscriptions = Get-AzSubscription -SubscriptionId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
    
    Set-AzContext -Subscription $subscriptions[0]
    
    $apiminstances = Get-AzApiManagement
    
    foreach (  $apim in $apiminstances) {
        $apimContext = New-AzApiManagementContext -ResourceGroupName $apim.ResourceGroupName -ServiceName $apim.Name
        $sId = Get-AzApiManagementSubscription -Context $apimContext
    
        foreach ($s in $sId) {
            $subKey = Get-AzApiManagementSubscriptionKey -Context $apimContext -SubscriptionId $s.SubscriptionId 
            if ($subKey.PrimaryKey -eq "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -or $subKey.SecondaryKey -eq "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") {
                $subtemp = Get-AzApiManagementSubscription -context $apimContext -SubscriptionId $s.SubscriptionId
                $user = Get-AzApiManagementUser -Context $apimContext -UserId $subtemp.UserId
                Write-Host "Subscription ID: " + $s.SubscriptionId + " User ID: " + $user.Id + " User Email: " + $user.Email
            }
        }
    }
    
        
    
    
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.