引入dll :Microsoft.IdentityModel.JsonWebTokens
1 string url = string.Format("{0}token", ConfigurationManager.AppSettings["AAdLoginUrl"]);
2 HttpClient client = new HttpClient();
3 var dic_Form = new Dictionary<string, string>();
4 dic_Form.Add("client_id", ConfigurationManager.AppSettings["Client_Id"]);
5 dic_Form.Add("scope", "openid");
6 dic_Form.Add("code", codestr);
7 dic_Form.Add("redirect_uri", ConfigurationManager.AppSettings["Redirect_Uri"]);
8 dic_Form.Add("grant_type", "authorization_code");
9 dic_Form.Add("client_secret", ConfigurationManager.AppSettings["Client_Secret"]);
10 var content = new FormUrlEncodedContent(dic_Form);
11 var response = client.PostAsync(url, content).Result;
12 resultStr = response.Content.ReadAsStringAsync().Result;
13 if (!string.IsNullOrEmpty(resultStr))
14 {
15 //ADDResultModel根据文档创建的返回信息实体类,需要自己根据需求创建
16 var result = JsonConvert.DeserializeObject<ADDResultModel>(resultStr);
17 if (!string.IsNullOrEmpty(result.error_description))
18 {
19 context.Response.Write(result.error_description);
20 }
21 if (!string.IsNullOrEmpty(result.access_token))
22 {
23 JsonWebTokenHandler handler = new JsonWebTokenHandler();
24 if (handler.CanReadToken(result.access_token))
25 {
26 JsonWebToken jwt = handler.ReadJsonWebToken(result.access_token);
27 var claims = jwt.Claims;
28 Claim claim1 = claims.Where(i => i.Type == "oid").FirstOrDefault();
29 Claim claim2 = claims.Where(i => i.Type == "unique_name").FirstOrDefault();
30 Console.WriteLine(claim1.Value);
31 Console.WriteLine(claim2.Value);
32 }
33 }
34 }