Login-AzureRmAccount (and related) equivalent(s) in .NET Azure SDK

前端 未结 1 1343
攒了一身酷
攒了一身酷 2020-12-22 03:12

I became to wonder, what would could the equivalents of

  • Login-AzureRmAccount
  • New-AzureRmADServicePrincipal
  • New-AzureRmADApplication
相关标签:
1条回答
  • 2020-12-22 03:32

    From the Azure Management Libraries for .NET source code, I couldn't find Creating AD ServicePrincipal and Azure AD function. After some investigation, I found we could do that with Microsoft.Azure.ActiveDirectory.GraphClient SDK. I do a test demo, it works correctly on my side. The following is my detail steps:

    Preparation:

    1.We need to create a native AD Application in the Azure portal

    1. Assign Access the directory as the signed-in user delegated permissions

    1. We could get our tenant Id that is Directory info on the screenshot portal

    Steps:

    1.Create a C# console project.

    2.Reference the Microsoft.Azure.ActiveDirectory.GraphClient SDK, more details please refer to packages.config section

    3.Add the following code in the project.

     public static async Task<string> GetAccessToken(string userName, string password)
            {
                var tokenResponse = await context.AcquireTokenAsync("https://graph.windows.net", appId, new UserCredential(userName, password));
                var accessToken = tokenResponse.AccessToken;
                return accessToken;
            }
    
        static string appId = "created AD Application Id";
        static string tenantId = "tenant Id";
        static string graphResourceId = "https://graph.windows.net";
        static string username = "user name";
        static string userPasswrod = "passowrd";
        static void Main(string[] args)
        {
    
            Uri servicePointUri = new Uri(graphResourceId);
            Uri serviceRoot = new Uri(servicePointUri, tenantId);
            ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, async () => await GetAccessToken(username, userPasswrod));
            Application application = new Application
            {  
                Homepage = "http://localhost:13526/",
                DisplayName = "tomnewapplication",
                IdentifierUris = new List<string> { "http://localhost/abcde" }
            };
    
         //Create Azure Directory Application   
         activeDirectoryClient.Applications.AddApplicationAsync(application).Wait();
            ServicePrincipal servicePrincipal = new ServicePrincipal
            {
                AppId = "existing AD application Id"
            };
         //Create service principal 
           activeDirectoryClient.ServicePrincipals.AddServicePrincipalAsync(servicePrincipal).Wait();
        }
    

    4. Check from azure portal

    packages.config file

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="Microsoft.Azure.ActiveDirectory.GraphClient" version="2.1.1" targetFramework="net452" />
      <package id="Microsoft.Data.Edm" version="5.6.4" targetFramework="net452" />
      <package id="Microsoft.Data.OData" version="5.6.4" targetFramework="net452" />
      <package id="Microsoft.Data.Services.Client" version="5.6.4" targetFramework="net452" />
      <package id="Microsoft.Graph" version="1.2.0" targetFramework="net452" />
      <package id="Microsoft.Graph.Core" version="1.3.0" targetFramework="net452" />
      <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.3" targetFramework="net452" />
      <package id="Newtonsoft.Json" version="6.0.1" targetFramework="net452" />
      <package id="System.Spatial" version="5.6.4" targetFramework="net452" />
    </packages>
    
    0 讨论(0)
提交回复
热议问题