How to login on Azure Portal using REST APIs










1














I plan to implement a C# app that will create Azure resources using REST APIs (API calls to Azure Resource Manager). When calling a REST API you have to authenticate by passing an authentication header "Authorization: Bearer yJ0eXAiOiJKV...".



How do I get this Bearer token? Looking online all that I found is having a Web App , you use its application_id. However i don't have any application and I don't want to create one.



I can replicate the calls that I intercept with Fiddler but I think that that is not the "recommended" way.



Have anyone faced this problem and has a solution?










share|improve this question























  • The access token described here seems to be the bearer token you're looking for: docs.microsoft.com/en-us/rest/api/apimanagement/…
    – Jacob Soderlund
    Nov 13 '18 at 0:05










  • Jacob, thank you for your answer. It seems that i haven't been explicit enough, I need to make API calls to Azure Resource Manager (i updated the question). The link you indicated seems to be about Management APIs, they clearly state there that it cannot be used for API calls to Azure Resource Manager.
    – Nicolae Daian
    Nov 13 '18 at 1:18















1














I plan to implement a C# app that will create Azure resources using REST APIs (API calls to Azure Resource Manager). When calling a REST API you have to authenticate by passing an authentication header "Authorization: Bearer yJ0eXAiOiJKV...".



How do I get this Bearer token? Looking online all that I found is having a Web App , you use its application_id. However i don't have any application and I don't want to create one.



I can replicate the calls that I intercept with Fiddler but I think that that is not the "recommended" way.



Have anyone faced this problem and has a solution?










share|improve this question























  • The access token described here seems to be the bearer token you're looking for: docs.microsoft.com/en-us/rest/api/apimanagement/…
    – Jacob Soderlund
    Nov 13 '18 at 0:05










  • Jacob, thank you for your answer. It seems that i haven't been explicit enough, I need to make API calls to Azure Resource Manager (i updated the question). The link you indicated seems to be about Management APIs, they clearly state there that it cannot be used for API calls to Azure Resource Manager.
    – Nicolae Daian
    Nov 13 '18 at 1:18













1












1








1







I plan to implement a C# app that will create Azure resources using REST APIs (API calls to Azure Resource Manager). When calling a REST API you have to authenticate by passing an authentication header "Authorization: Bearer yJ0eXAiOiJKV...".



How do I get this Bearer token? Looking online all that I found is having a Web App , you use its application_id. However i don't have any application and I don't want to create one.



I can replicate the calls that I intercept with Fiddler but I think that that is not the "recommended" way.



Have anyone faced this problem and has a solution?










share|improve this question















I plan to implement a C# app that will create Azure resources using REST APIs (API calls to Azure Resource Manager). When calling a REST API you have to authenticate by passing an authentication header "Authorization: Bearer yJ0eXAiOiJKV...".



How do I get this Bearer token? Looking online all that I found is having a Web App , you use its application_id. However i don't have any application and I don't want to create one.



I can replicate the calls that I intercept with Fiddler but I think that that is not the "recommended" way.



Have anyone faced this problem and has a solution?







azure azure-active-directory azure-resource-manager






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 1:15







Nicolae Daian

















asked Nov 12 '18 at 23:51









Nicolae DaianNicolae Daian

1399




1399











  • The access token described here seems to be the bearer token you're looking for: docs.microsoft.com/en-us/rest/api/apimanagement/…
    – Jacob Soderlund
    Nov 13 '18 at 0:05










  • Jacob, thank you for your answer. It seems that i haven't been explicit enough, I need to make API calls to Azure Resource Manager (i updated the question). The link you indicated seems to be about Management APIs, they clearly state there that it cannot be used for API calls to Azure Resource Manager.
    – Nicolae Daian
    Nov 13 '18 at 1:18
















  • The access token described here seems to be the bearer token you're looking for: docs.microsoft.com/en-us/rest/api/apimanagement/…
    – Jacob Soderlund
    Nov 13 '18 at 0:05










  • Jacob, thank you for your answer. It seems that i haven't been explicit enough, I need to make API calls to Azure Resource Manager (i updated the question). The link you indicated seems to be about Management APIs, they clearly state there that it cannot be used for API calls to Azure Resource Manager.
    – Nicolae Daian
    Nov 13 '18 at 1:18















The access token described here seems to be the bearer token you're looking for: docs.microsoft.com/en-us/rest/api/apimanagement/…
– Jacob Soderlund
Nov 13 '18 at 0:05




The access token described here seems to be the bearer token you're looking for: docs.microsoft.com/en-us/rest/api/apimanagement/…
– Jacob Soderlund
Nov 13 '18 at 0:05












Jacob, thank you for your answer. It seems that i haven't been explicit enough, I need to make API calls to Azure Resource Manager (i updated the question). The link you indicated seems to be about Management APIs, they clearly state there that it cannot be used for API calls to Azure Resource Manager.
– Nicolae Daian
Nov 13 '18 at 1:18




Jacob, thank you for your answer. It seems that i haven't been explicit enough, I need to make API calls to Azure Resource Manager (i updated the question). The link you indicated seems to be about Management APIs, they clearly state there that it cannot be used for API calls to Azure Resource Manager.
– Nicolae Daian
Nov 13 '18 at 1:18












2 Answers
2






active

oldest

votes


















1














Short answer: If you're developing a C# application that is going to use Azure REST APIs, then in order to get the bearer token for authentication you do need to have an Azure AD application registration (no way around that, as it's required for you to be able to authenticate using any of the supported OAuth 2.0 grant flows).




There are a few ways to make things more convenient for you though:




  • Use CLI to create a service principal for RBAC



    From Azure Portal, open up the CLI by clicking on highlighted icon.
    enter image description here



    Now run below mentioned command



    az ad sp create-for-rbac -n "MyTestSPForAzureRESTAPIs"


    This does multiple things for you in a single command and provides a great way to get started with testing the REST APIs.



    The created service principal is added as a "Contributor" to your Azure subscription. You can always go to Subscriptions > Your Subscription > Access control (IAM) and change that as per your requirements.



    You get an application ID as well as Password/client secret that you can then use in C# code to get bearer token.



    Sample output
    enter image description here



    NOTE: Since this approach gives you a client secret, you should use this only from server side applications (like a web API or Web App or Daemon service). Do NOT use client secrets from a desktop based app (like console app or WPF app) or SPA in a production scenario.



    I say this because desktop based apps or SPAs are not secure enough to handle client secrets and there are other different authentication flows recommended for them. If your case happens to be any of those, look at delegated permissions from your Azure AD application where you can prompt an end user for credentials. Just comment on the answer and I can add more specific guidance around those.




  • Use Managed Identity in case of App Service or Azure Function



    If you plan to host the C# application that you mention, using App Service or as an Azure Function, then you can make use of MSI. Even in this case an application will be created in Azure AD, but you don't need to do that or manage keys (change them regularly etc.). It's a great option, highly recommended if it suits your scenario.



    Read here for more details: How to use managed identities for App Service and Azure Functions







share|improve this answer






















  • Thank you Rohit for your answer! I was thinking to use the Service Principal in my desktop app but store it encrypted on the machine. The app will decrypt the secret and use it.
    – Nicolae Daian
    Nov 13 '18 at 16:31


















1














If you just want to get the bearer token. I recommand that you could login in your account in the Azure API document. After we login then we could get the bearer token.



enter image description here



enter image description here



If we want to use code to get access token to access or modify resources, create an identity for the Azure AD application is required . This identity is known as a service principal. Then we can then assign the required permissions to the service principal.



How to registry an Azure AD application and assign role to the application, please refer to this document.



The following is demo code how to get the access token with applicationId and sercet key



public static async Task<string> GetAccessToken(string tenantId, string clientId, string clientSecretKey)


var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
ClientCredential clientCredential = new ClientCredential(clientId, clientSecretKey);
var tokenResponse = await context.AcquireTokenAsync("https://management.azure.com/", clientCredential);
var accessToken = tokenResponse.AccessToken;
return accessToken;






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271777%2fhow-to-login-on-azure-portal-using-rest-apis%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Short answer: If you're developing a C# application that is going to use Azure REST APIs, then in order to get the bearer token for authentication you do need to have an Azure AD application registration (no way around that, as it's required for you to be able to authenticate using any of the supported OAuth 2.0 grant flows).




    There are a few ways to make things more convenient for you though:




    • Use CLI to create a service principal for RBAC



      From Azure Portal, open up the CLI by clicking on highlighted icon.
      enter image description here



      Now run below mentioned command



      az ad sp create-for-rbac -n "MyTestSPForAzureRESTAPIs"


      This does multiple things for you in a single command and provides a great way to get started with testing the REST APIs.



      The created service principal is added as a "Contributor" to your Azure subscription. You can always go to Subscriptions > Your Subscription > Access control (IAM) and change that as per your requirements.



      You get an application ID as well as Password/client secret that you can then use in C# code to get bearer token.



      Sample output
      enter image description here



      NOTE: Since this approach gives you a client secret, you should use this only from server side applications (like a web API or Web App or Daemon service). Do NOT use client secrets from a desktop based app (like console app or WPF app) or SPA in a production scenario.



      I say this because desktop based apps or SPAs are not secure enough to handle client secrets and there are other different authentication flows recommended for them. If your case happens to be any of those, look at delegated permissions from your Azure AD application where you can prompt an end user for credentials. Just comment on the answer and I can add more specific guidance around those.




    • Use Managed Identity in case of App Service or Azure Function



      If you plan to host the C# application that you mention, using App Service or as an Azure Function, then you can make use of MSI. Even in this case an application will be created in Azure AD, but you don't need to do that or manage keys (change them regularly etc.). It's a great option, highly recommended if it suits your scenario.



      Read here for more details: How to use managed identities for App Service and Azure Functions







    share|improve this answer






















    • Thank you Rohit for your answer! I was thinking to use the Service Principal in my desktop app but store it encrypted on the machine. The app will decrypt the secret and use it.
      – Nicolae Daian
      Nov 13 '18 at 16:31















    1














    Short answer: If you're developing a C# application that is going to use Azure REST APIs, then in order to get the bearer token for authentication you do need to have an Azure AD application registration (no way around that, as it's required for you to be able to authenticate using any of the supported OAuth 2.0 grant flows).




    There are a few ways to make things more convenient for you though:




    • Use CLI to create a service principal for RBAC



      From Azure Portal, open up the CLI by clicking on highlighted icon.
      enter image description here



      Now run below mentioned command



      az ad sp create-for-rbac -n "MyTestSPForAzureRESTAPIs"


      This does multiple things for you in a single command and provides a great way to get started with testing the REST APIs.



      The created service principal is added as a "Contributor" to your Azure subscription. You can always go to Subscriptions > Your Subscription > Access control (IAM) and change that as per your requirements.



      You get an application ID as well as Password/client secret that you can then use in C# code to get bearer token.



      Sample output
      enter image description here



      NOTE: Since this approach gives you a client secret, you should use this only from server side applications (like a web API or Web App or Daemon service). Do NOT use client secrets from a desktop based app (like console app or WPF app) or SPA in a production scenario.



      I say this because desktop based apps or SPAs are not secure enough to handle client secrets and there are other different authentication flows recommended for them. If your case happens to be any of those, look at delegated permissions from your Azure AD application where you can prompt an end user for credentials. Just comment on the answer and I can add more specific guidance around those.




    • Use Managed Identity in case of App Service or Azure Function



      If you plan to host the C# application that you mention, using App Service or as an Azure Function, then you can make use of MSI. Even in this case an application will be created in Azure AD, but you don't need to do that or manage keys (change them regularly etc.). It's a great option, highly recommended if it suits your scenario.



      Read here for more details: How to use managed identities for App Service and Azure Functions







    share|improve this answer






















    • Thank you Rohit for your answer! I was thinking to use the Service Principal in my desktop app but store it encrypted on the machine. The app will decrypt the secret and use it.
      – Nicolae Daian
      Nov 13 '18 at 16:31













    1












    1








    1






    Short answer: If you're developing a C# application that is going to use Azure REST APIs, then in order to get the bearer token for authentication you do need to have an Azure AD application registration (no way around that, as it's required for you to be able to authenticate using any of the supported OAuth 2.0 grant flows).




    There are a few ways to make things more convenient for you though:




    • Use CLI to create a service principal for RBAC



      From Azure Portal, open up the CLI by clicking on highlighted icon.
      enter image description here



      Now run below mentioned command



      az ad sp create-for-rbac -n "MyTestSPForAzureRESTAPIs"


      This does multiple things for you in a single command and provides a great way to get started with testing the REST APIs.



      The created service principal is added as a "Contributor" to your Azure subscription. You can always go to Subscriptions > Your Subscription > Access control (IAM) and change that as per your requirements.



      You get an application ID as well as Password/client secret that you can then use in C# code to get bearer token.



      Sample output
      enter image description here



      NOTE: Since this approach gives you a client secret, you should use this only from server side applications (like a web API or Web App or Daemon service). Do NOT use client secrets from a desktop based app (like console app or WPF app) or SPA in a production scenario.



      I say this because desktop based apps or SPAs are not secure enough to handle client secrets and there are other different authentication flows recommended for them. If your case happens to be any of those, look at delegated permissions from your Azure AD application where you can prompt an end user for credentials. Just comment on the answer and I can add more specific guidance around those.




    • Use Managed Identity in case of App Service or Azure Function



      If you plan to host the C# application that you mention, using App Service or as an Azure Function, then you can make use of MSI. Even in this case an application will be created in Azure AD, but you don't need to do that or manage keys (change them regularly etc.). It's a great option, highly recommended if it suits your scenario.



      Read here for more details: How to use managed identities for App Service and Azure Functions







    share|improve this answer














    Short answer: If you're developing a C# application that is going to use Azure REST APIs, then in order to get the bearer token for authentication you do need to have an Azure AD application registration (no way around that, as it's required for you to be able to authenticate using any of the supported OAuth 2.0 grant flows).




    There are a few ways to make things more convenient for you though:




    • Use CLI to create a service principal for RBAC



      From Azure Portal, open up the CLI by clicking on highlighted icon.
      enter image description here



      Now run below mentioned command



      az ad sp create-for-rbac -n "MyTestSPForAzureRESTAPIs"


      This does multiple things for you in a single command and provides a great way to get started with testing the REST APIs.



      The created service principal is added as a "Contributor" to your Azure subscription. You can always go to Subscriptions > Your Subscription > Access control (IAM) and change that as per your requirements.



      You get an application ID as well as Password/client secret that you can then use in C# code to get bearer token.



      Sample output
      enter image description here



      NOTE: Since this approach gives you a client secret, you should use this only from server side applications (like a web API or Web App or Daemon service). Do NOT use client secrets from a desktop based app (like console app or WPF app) or SPA in a production scenario.



      I say this because desktop based apps or SPAs are not secure enough to handle client secrets and there are other different authentication flows recommended for them. If your case happens to be any of those, look at delegated permissions from your Azure AD application where you can prompt an end user for credentials. Just comment on the answer and I can add more specific guidance around those.




    • Use Managed Identity in case of App Service or Azure Function



      If you plan to host the C# application that you mention, using App Service or as an Azure Function, then you can make use of MSI. Even in this case an application will be created in Azure AD, but you don't need to do that or manage keys (change them regularly etc.). It's a great option, highly recommended if it suits your scenario.



      Read here for more details: How to use managed identities for App Service and Azure Functions








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 1:58

























    answered Nov 13 '18 at 1:50









    Rohit SaigalRohit Saigal

    2,8072216




    2,8072216











    • Thank you Rohit for your answer! I was thinking to use the Service Principal in my desktop app but store it encrypted on the machine. The app will decrypt the secret and use it.
      – Nicolae Daian
      Nov 13 '18 at 16:31
















    • Thank you Rohit for your answer! I was thinking to use the Service Principal in my desktop app but store it encrypted on the machine. The app will decrypt the secret and use it.
      – Nicolae Daian
      Nov 13 '18 at 16:31















    Thank you Rohit for your answer! I was thinking to use the Service Principal in my desktop app but store it encrypted on the machine. The app will decrypt the secret and use it.
    – Nicolae Daian
    Nov 13 '18 at 16:31




    Thank you Rohit for your answer! I was thinking to use the Service Principal in my desktop app but store it encrypted on the machine. The app will decrypt the secret and use it.
    – Nicolae Daian
    Nov 13 '18 at 16:31













    1














    If you just want to get the bearer token. I recommand that you could login in your account in the Azure API document. After we login then we could get the bearer token.



    enter image description here



    enter image description here



    If we want to use code to get access token to access or modify resources, create an identity for the Azure AD application is required . This identity is known as a service principal. Then we can then assign the required permissions to the service principal.



    How to registry an Azure AD application and assign role to the application, please refer to this document.



    The following is demo code how to get the access token with applicationId and sercet key



    public static async Task<string> GetAccessToken(string tenantId, string clientId, string clientSecretKey)


    var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
    ClientCredential clientCredential = new ClientCredential(clientId, clientSecretKey);
    var tokenResponse = await context.AcquireTokenAsync("https://management.azure.com/", clientCredential);
    var accessToken = tokenResponse.AccessToken;
    return accessToken;






    share|improve this answer



























      1














      If you just want to get the bearer token. I recommand that you could login in your account in the Azure API document. After we login then we could get the bearer token.



      enter image description here



      enter image description here



      If we want to use code to get access token to access or modify resources, create an identity for the Azure AD application is required . This identity is known as a service principal. Then we can then assign the required permissions to the service principal.



      How to registry an Azure AD application and assign role to the application, please refer to this document.



      The following is demo code how to get the access token with applicationId and sercet key



      public static async Task<string> GetAccessToken(string tenantId, string clientId, string clientSecretKey)


      var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
      ClientCredential clientCredential = new ClientCredential(clientId, clientSecretKey);
      var tokenResponse = await context.AcquireTokenAsync("https://management.azure.com/", clientCredential);
      var accessToken = tokenResponse.AccessToken;
      return accessToken;






      share|improve this answer

























        1












        1








        1






        If you just want to get the bearer token. I recommand that you could login in your account in the Azure API document. After we login then we could get the bearer token.



        enter image description here



        enter image description here



        If we want to use code to get access token to access or modify resources, create an identity for the Azure AD application is required . This identity is known as a service principal. Then we can then assign the required permissions to the service principal.



        How to registry an Azure AD application and assign role to the application, please refer to this document.



        The following is demo code how to get the access token with applicationId and sercet key



        public static async Task<string> GetAccessToken(string tenantId, string clientId, string clientSecretKey)


        var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
        ClientCredential clientCredential = new ClientCredential(clientId, clientSecretKey);
        var tokenResponse = await context.AcquireTokenAsync("https://management.azure.com/", clientCredential);
        var accessToken = tokenResponse.AccessToken;
        return accessToken;






        share|improve this answer














        If you just want to get the bearer token. I recommand that you could login in your account in the Azure API document. After we login then we could get the bearer token.



        enter image description here



        enter image description here



        If we want to use code to get access token to access or modify resources, create an identity for the Azure AD application is required . This identity is known as a service principal. Then we can then assign the required permissions to the service principal.



        How to registry an Azure AD application and assign role to the application, please refer to this document.



        The following is demo code how to get the access token with applicationId and sercet key



        public static async Task<string> GetAccessToken(string tenantId, string clientId, string clientSecretKey)


        var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
        ClientCredential clientCredential = new ClientCredential(clientId, clientSecretKey);
        var tokenResponse = await context.AcquireTokenAsync("https://management.azure.com/", clientCredential);
        var accessToken = tokenResponse.AccessToken;
        return accessToken;







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 5:20

























        answered Nov 13 '18 at 1:16









        Tom SunTom Sun

        16.3k2921




        16.3k2921



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271777%2fhow-to-login-on-azure-portal-using-rest-apis%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3