Basically I'm implementing an SSO for an employee portal that I'm making for us, but I'd also like to be able to access the graph API (At the least, the AzureAD REST API items, like adding/removing/getting users information) without having to be signed in through an SSO.
This way, I can use what I'm thinking to be some kind of API key/secret sort of setup and schedule cron jobs that interact with the AD in some way. Technically I could signin and set my account to be the one running this, but that seems kind of hacky and unreliable (as in, if something happens to my account, password expires, changes, etc... then the refresh token will no longer be valid and I'll have to sign in again and the task could be temporarily broken.)
I could have sworn I've seen documentation for this somewhere when I was looking into implementing this a few months ago, but I can't for the life of me find it now.
Really hope this isn't a duplicate, I just can't think of wording to search for that doesn't keep coming up with SSO-based API information.
Update - Alright, it looks like I figured this out (with the help of Shaun Luttin's answer posted below: https://stackoverflow.com/a/32618417/3721165 [link for convenience])
So, all of the info Shaun brought together was really helpful. Initially the docs were fairly confusing due to how they word things, as well as the complexity of some of their examples. But once I got to digging through some of the examples, plus some of the info Shaun provided, and some experimenting/research on my own, I was able to come up with this (basic demo/concept):
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;
}
}
}
I found through further research that in order to use the Graph API the way I'm intending, you have to provide the graph resource URL (https://graph.windows.net) to the AquireToken method, instead of your app ID/URL.
So, I'm accepting Shaun's answer, but I also wanted to give my working result of that answer.
Thanks for the help, guys!