.NET Azure SDK中的Login-AzureRmAccount(及相关)等效项 [英] Login-AzureRmAccount (and related) equivalent(s) in .NET Azure SDK

查看:86
本文介绍了.NET Azure SDK中的Login-AzureRmAccount(及相关)等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始怀疑,

  • Login-AzureRmAccount
  • New-AzureRmADServicePrincipal
  • New-AzureRmADApplication

适用于.NET的Azure SDK 中.由于某些原因,我似乎无法找到它们,并且我想执行类似

in Azure SDK for .NET. For some reason I don't seem to be able to locate them and I would like to do something like in this blog post, but in code.

<2017-06-20 00:42

<edit: 2017-06-20 00:42

Tom Sun 中得到一些提示,并进行了更深入的探讨,我找到了一个答案,该答案部分解决了旧库"问题,也解决了最初选择订阅的问题.在 https://stackoverflow.com/a/41360632/1332416 中进行了描述,但是该代码也已经有点旧了. ,再进一步说一下,在 https://stackoverflow.com/a/38036598/1332416 .但是,这还不足够,我会继续戳一些(除非有人戳些).我想我对原始问题的表述不准确.我想重新创建使用PowerShell的常规登录流程",但这一次是在代码中.但是,这些PS命令很难确定. :)

Taking some cues from Tom Sun and poking this a bit deeper, I found an answer that solves partially a problem of "old libraries" and also the one initially choosing a subscription. It's described at https://stackoverflow.com/a/41360632/1332416, but that code is already a bit old too, and poking a bit further, there's a re-write of that into a bit newer form at https://stackoverflow.com/a/38036598/1332416. However, this isn't quite there yet, I keep poking a bit further (unless someone pokes further). I think I rephrased the original question unprecisely. I'd like to re-create "the usual log-in flow with PowerShell", but this time in code. These PS commands are a bit rough to pin down, though. :)

有关使用PowerShell选择订阅的部分可能是这样的: $subscription = Get-AzureRmSubscription | Out-GridView -Title "Select the subsbcription for the deployment" -PassThru Select-AzureRmSubscription -SubscriptionId $subscription.SubscriptionId

The part about choosing a subscription using PowerShell could be like this: $subscription = Get-AzureRmSubscription | Out-GridView -Title "Select the subsbcription for the deployment" -PassThru Select-AzureRmSubscription -SubscriptionId $subscription.SubscriptionId

推荐答案

来自 Microsoft.Azure.ActiveDirectory做到这一点. GraphClient SDK.我做了一个测试演示,它可以正常工作.以下是我的详细步骤:

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:

准备工作

1.我们需要在Azure门户中创建一个本地 AD应用程序

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

  1. 分配以登录用户身份访问目录委派权限
  1. Assign Access the directory as the signed-in user delegated permissions

  1. 我们可以在屏幕截图中获取我们的租户ID,即目录信息 门户
  1. We could get our tenant Id that is Directory info on the screenshot portal

步骤:

1.创建一个C#控制台项目.

1.Create a C# console project.

2.参考 Microsoft.Azure.ActiveDirectory.GraphClient SDK,更多详细信息,请参阅packages.config部分

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

3.在项目中添加以下代码.

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.从天蓝色门户网站进行检查

4. Check from azure portal

packages.config文件

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>

这篇关于.NET Azure SDK中的Login-AzureRmAccount(及相关)等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆