在.NET Azure SDK中登录AzurerAccount(及相关)等效帐户
我开始纳闷,那会是什么在.NET Azure SDK中登录AzurerAccount(及相关)等效帐户,azure,azure-active-directory,azure-sdk-.net,azure-ad-graph-api,msal,Azure,Azure Active Directory,Azure Sdk .net,Azure Ad Graph Api,Msal,我开始纳闷,那会是什么 登录AzureRmAccount 新AzureRmADServicePrincipal 新AzureRmADApplication 在。出于某种原因,我似乎无法找到它们,我想做一些类似的事情,但在代码中 从源代码中,我找不到创建广告服务主体和Azure广告功能。 经过一些调查,我发现我们可以用SDK实现这一点。我做了一个测试演示,它在我这边正常工作。以下是我的详细步骤: 准备: 1.我们需要在Azure门户中创建一个本地广告应用程序 分配以登录用户身份访问目录委派权
- 登录AzureRmAccount
- 新AzureRmADServicePrincipal
- 新AzureRmADApplication
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();
}
2.参考SDK,更多详细信息请参考packages.config部分
3.在项目中添加以下代码
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();
}
公共静态异步任务GetAccessToken(字符串用户名、字符串密码)
{
var tokenResponse=await context.AcquireTokenAsync(“https://graph.windows.net,appId,新用户凭证(用户名、密码));
var accessToken=tokenResponse.accessToken;
返回accessToken;
}
静态字符串appId=“已创建广告应用程序Id”;
静态字符串tenantId=“租户Id”;
静态字符串graphResourceId=”https://graph.windows.net";
静态字符串username=“user name”;
静态字符串userPasswrod=“passowrd”;
静态void Main(字符串[]参数)
{
Uri servicePointUri=新Uri(graphResourceId);
Uri serviceRoot=新Uri(servicePointUri,tenantId);
ActiveDirectoryClient ActiveDirectoryClient=新的ActiveDirectoryClient(serviceRoot,async()=>Wait GetAccessToken(username,userPasswrod));
应用程序=新应用程序
{
主页=”http://localhost:13526/",
DisplayName=“tomnewapplication”,
IdentifierUris=新列表{”http://localhost/abcde" }
};
//创建Azure目录应用程序
activeDirectoryClient.Applications.AddApplicationAsync(application.Wait();
ServicePrincipal ServicePrincipal=新的ServicePrincipal
{
AppId=“现有广告应用程序Id”
};
//创建服务主体
activeDirectoryClient.ServicePrincipals.AddServicePrincipalsync(servicePrincipal.Wait();
}
四,。从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>
Hmm,很有趣。我想知道那个图书馆是否更新了。好的方面,我会在几个小时后回到电脑上,让我玩一下这个。租户ID可能会被查询为使用Get AzureRmSubscription
,然后选择一个,但也可能会使用看起来有点不更新的Nuget包。。。看起来要去……)我已更新答案以添加包信息。请尝试使用SDK而不是SDK。我很感激您已经经历的麻烦。我有点犹豫是否接受这个答案,尽管这个答案很好,因为这些库已经被弃用了,尽管我发现我在选择订阅时忽略了提供PS commandlet,但当翻译成C#代码时,它与“标准流”中的其他命令一样有问题。我用更多的研究更新了这个问题,我计划在这个问题上再深入一点。如果什么都没有出现,我会接受这个(可能明天)。这一切都很好,我会更深入地挖掘,如果我有更多的时间来挖掘这个,也许会发布一个新的、更好的问题。谢谢你的麻烦!嘿,我正在尝试使用Azure RM.Net库,它需要AzureCredential对象进行身份验证。我想知道是否有任何方法可以在不创建Azure广告应用程序的情况下,通过传递用户名和密码(类似于Connect AzurerAccount)生成此内容。