基本上,我正在为我们为我们制作的员工门户实施SSO,但我也希望能够访问图形API(至少,AzureAD REST API项目,如添加/删除/获取用户信息),而无需通过SSO登录。如何在没有单一登录的情况下使用Azure AD
这样,我可以使用我正在考虑的某种API密钥/秘密种类的设置和计划与某些方式与AD进行交互的cron作业。从技术上讲,我可以登录并设置我的账户为运行此账户的账户,但这看起来有点不可靠(例如,如果我的账户出现问题,密码过期,更改等等,那么刷新令牌将不再是有效的,我将不得不再次登录,任务可能会暂时中断。)
我可以发誓我在几个月前正在考虑实施此操作时看到过有关此文档,但我不能为我现在的生活找到它。
真的希望这不是重复的,我只是不能想到的措辞搜索,不会提供基于SSO的API信息。
更新 - 好吧,它看起来像我想通了这一点(与肖恩Luttin的答案的帮助下贴:https://stackoverflow.com/a/32618417/3721165 [链接为了方便])
所以,所有的信息肖恩汇聚真的很有帮助。最初这些文档由于他们如何说词而相当混乱,以及其中一些例子的复杂性。但是,一旦我通过一些例子挖掘,再加上一些肖恩提供的信息,并以我个人的一些试验/研究的,我能想出这个(基本演示/概念):
using System;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Azure.ActiveDirectory.GraphClient;
namespace AzureADGraphApi
{
class Program
{
private static string tenant = "...tenant id...";
private static string clientid = "...client id...";
private static string appkey = "...app key...";
private static string aadinstance = "https://login.microsoftonline.com/{0}";
private static string graphResourceUrl = "https://graph.windows.net";
static void Main(string[] args)
{
Uri serviceRoot = new Uri(graphResourceUrl + "/" + tenant);
ActiveDirectoryClient adc = new ActiveDirectoryClient(serviceRoot, async() => await GetToken());
IPagedCollection<IUser> Users = adc.Users.ExecuteAsync().Result;
bool pagesLeft = false;
do
{
foreach (IUser user in Users.CurrentPage)
{
Console.WriteLine(user.DisplayName);
}
pagesLeft = Users.MorePagesAvailable;
Users = Users.GetNextPageAsync().Result;
Console.WriteLine("--- Page Break ---");
} while (pagesLeft);
Console.ReadLine();
}
private static async Task<string> GetToken()
{
AuthenticationContext authContext = new AuthenticationContext(String.Format(CultureInfo.InvariantCulture, aadinstance, tenant));
AuthenticationResult result = authContext.AcquireToken(graphResourceUrl, new ClientCredential(clientid, appkey));
return result.AccessToken;
}
}
}
我通过进一步的研究发现,为了按照我的意图使用图形API,您必须将图形资源URL(https://graph.windows.net)提供给AquireToken方法,而不是您的应用程序ID/URL。
所以,我接受肖恩的答案,但我也想给我的答案的工作结果。
感谢您的帮助,伙计们!
是你想以编程方式或仅通过用户界面来执行此操作?如果您正在寻找仅用户界面解决方案,那么您最好在ServerFault或非编程论坛上提问。 –
@ShaunLuttin编程。我基本上需要编写一个服务来执行(最好在PHP中使用一个),以便在AzureAD用户和其他各种事情上维护一些自定义信息。我也看到了你的回答,但我实际上只是回家过夜(5:03在这里,耶!),所以明天早上我会检查一下。谢谢btw! –