blob: 3943cc8c969b6655942fcc3970c67a5efe80d04e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
namespace OAuthClient {
using System;
using System.Configuration;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Web;
using System.Web.UI;
using DotNetOpenAuth.ApplicationBlock;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2;
public partial class AzureAD : System.Web.UI.Page {
private static readonly AzureADClient client = new AzureADClient {
ClientIdentifier = ConfigurationManager.AppSettings["AzureADAppID"],
ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["AzureADAppSecret"]),
};
protected void Page_Load(object sender, EventArgs e) {
this.RegisterAsyncTask(
new PageAsyncTask(
async ct => {
IAuthorizationState authorization = await client.ProcessUserAuthorizationAsync(new HttpRequestWrapper(Request), ct);
if (authorization == null) {
// Kick off authorization request
var request = await client.PrepareRequestUserAuthorizationAsync(cancellationToken: ct);
await request.SendAsync();
this.Context.Response.End();
} else {
string token = authorization.AccessToken;
AzureADClaims claimsAD = client.ParseAccessToken(token);
// Request to AD needs a {tenantid}/users/{userid}
var request =
WebRequest.Create("https://graph.windows.net/" + claimsAD.Tid + "/users/" + claimsAD.Oid + "?api-version=0.9");
request.Headers = new WebHeaderCollection();
request.Headers.Add("authorization", token);
using (var response = request.GetResponse()) {
using (var responseStream = response.GetResponseStream()) {
var serializer = new DataContractJsonSerializer(typeof(AzureADGraph));
AzureADGraph graphData = (AzureADGraph)serializer.ReadObject(responseStream);
this.nameLabel.Text = HttpUtility.HtmlEncode(graphData.DisplayName);
}
}
}
}));
}
}
}
|