Skip to content

Instantly share code, notes, and snippets.

@garima2510
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garima2510/1f68dadbb75e09fd1fe6 to your computer and use it in GitHub Desktop.
Save garima2510/1f68dadbb75e09fd1fe6 to your computer and use it in GitHub Desktop.
public class HomeController : Controller
{
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"]; //clientId
private static string appKey = ConfigurationManager.AppSettings["ida:AppKey"]; //clientSecret
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"]; //https://login.windows.net/{0}
private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"]; //TenantName.onmicrosoft.com
private const string ResourceUri = "https://onenote.com";
public static readonly string Authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);//https://login.windows.net/TenantName.onmicrosoft.com
Uri returnUri = new Uri("https://localhost:44327/Home/ReturnFlow");
public ActionResult Index()
{
string authorizationUrl = string.Format(
aadInstance + "/oauth2/authorize?api-version=1.0&response_type=code&client_id={1}&resource={2}&redirect_uri={3}",
tenant,
clientId,
ResourceUri,
returnUri.AbsoluteUri
);
/*
https://login.windows.net/TenantName.onmicrosoft.com/oauth2/authorize?api-version=1.0&response_type=code
&client_id=9eaffd31-8592-4c96-892c-32adb9a432b8&resource=https://onenote.com&redirect_uri=https://localhost:44327/Home/ReturnFlow
*/
return new RedirectResult(authorizationUrl);
}
public async Task<ActionResult> ReturnFlow(string code)
{
AuthenticationContext AuthContext = new AuthenticationContext(Authority);
ClientCredential credential = new ClientCredential(clientId, appKey);
var result = await AuthContext.AcquireTokenByAuthorizationCodeAsync(code, returnUri, credential);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
string apiRoute = "https://www.onenote.com/api/beta/me/notes/notebooks";
var getMessage = new HttpRequestMessage(HttpMethod.Get, apiRoute);
HttpResponseMessage response = await client.SendAsync(getMessage);
string data = await response.Content.ReadAsStringAsync();
return Json(data, JsonRequestBehavior.AllowGet);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment