.NET Azure SDK 中的 Login-AzureRmAccount(及相关)等效项
Login-AzureRmAccount (and related) equivalent(s) in .NET Azure SDK
我想知道,
的等价物会是什么?
- 登录-AzureRmAccount
- 新 AzureRmADServicePrincipal
- 新建 AzureRmADApplication
在 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 中,但在代码中。
<编辑:2017-06-20 00:42
从 Tom Sun 那里得到一些启发并深入研究,我找到了一个答案,该答案部分解决了 "old libraries" 的问题,也解决了最初选择订阅。 , 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 中对其进行了描述。然而,这还不完全,我继续深入研究(除非有人进一步探究)。我想我不准确地改写了原来的问题。我想重新创建 "the usual log-in flow with PowerShell",但这次是在代码中。不过,这些 PS 命令有点难以确定。 :)
关于使用 PowerShell 选择订阅的部分可能是这样的:
$subscription = Get-AzureRmSubscription | Out-GridView -Title "Select the subsbcription for the deployment" -PassThru
Select-AzureRmSubscription -SubscriptionId $subscription.SubscriptionId
从 Azure Management Libraries for .NET 源代码中,我找不到 Creating AD ServicePrincipal 和 Azure AD 函数。
经过一些调查,我发现我们可以使用 Microsoft.Azure.ActiveDirectory.GraphClient SDK 来做到这一点。我做了一个测试演示,它在我这边工作正常。以下是我的详细步骤:
准备:
1.We 需要在 Azure 门户中创建 native AD 应用程序
- 分配以登录用户身份访问目录委派权限
- 我们可以在屏幕截图中获取我们的租户 ID,即 Directory 信息
传送门
步骤:
1.Create 一个 C# 控制台项目。
2.Reference Microsoft.Azure.ActiveDirectory.GraphClient SDK,更多详情请参考packages.config部分
3.Add项目中的以下代码。
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。从 Azure 门户检查
packages.config 文件
<?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>
我想知道,
的等价物会是什么?- 登录-AzureRmAccount
- 新 AzureRmADServicePrincipal
- 新建 AzureRmADApplication
在 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 中,但在代码中。
<编辑:2017-06-20 00:42
从 Tom Sun 那里得到一些启发并深入研究,我找到了一个答案,该答案部分解决了 "old libraries" 的问题,也解决了最初选择订阅。
关于使用 PowerShell 选择订阅的部分可能是这样的:
$subscription = Get-AzureRmSubscription | Out-GridView -Title "Select the subsbcription for the deployment" -PassThru
Select-AzureRmSubscription -SubscriptionId $subscription.SubscriptionId
从 Azure Management Libraries for .NET 源代码中,我找不到 Creating AD ServicePrincipal 和 Azure AD 函数。 经过一些调查,我发现我们可以使用 Microsoft.Azure.ActiveDirectory.GraphClient SDK 来做到这一点。我做了一个测试演示,它在我这边工作正常。以下是我的详细步骤:
准备:
1.We 需要在 Azure 门户中创建 native AD 应用程序
- 分配以登录用户身份访问目录委派权限
- 我们可以在屏幕截图中获取我们的租户 ID,即 Directory 信息 传送门
步骤:
1.Create 一个 C# 控制台项目。
2.Reference Microsoft.Azure.ActiveDirectory.GraphClient SDK,更多详情请参考packages.config部分
3.Add项目中的以下代码。
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。从 Azure 门户检查
packages.config 文件
<?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>