Last active
August 29, 2015 14:26
-
-
Save garima2510/1f68dadbb75e09fd1fe6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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