诗情寻知己
揽几缕、轻挽起,暮暮朝朝与君语。
 引入dll :Microsoft.IdentityModel.JsonWebTokens

 代码如下:

 1             try
 2             {
 3                 context.Response.ContentType = "text/plain";
 4                 var codestr = context.Request["code"];
 5                 var state = context.Request["state"];
 6                 string resultStr = string.Empty;
 7                 if (codestr != null)
 8                 {
 9                     string url = string.Format("{0}token", ConfigurationManager.AppSettings["AddLoginUrl"]);
10                     HttpClient client = new HttpClient();
11                     var dic = new Dictionary<string, string>();
12                     dic.Add("client_id", ConfigurationManager.AppSettings["Client_Id"]);
13                     dic.Add("scope", "openid");
14                     dic.Add("code", codestr);
15                     dic.Add("redirect_uri", ConfigurationManager.AppSettings["Redirect_Uri"]);
16                     dic.Add("grant_type", "authorization_code");
17                     dic.Add("client_secret", ConfigurationManager.AppSettings["Client_Secret"]);
18                     var content = new FormUrlEncodedContent(dic);
19                     var response = client.PostAsync(url, content).Result;
20                     resultStr = response.Content.ReadAsStringAsync().Result;
21                     if (!string.IsNullOrEmpty(resultStr))
22                     {
23                         var result = JsonConvert.DeserializeObject<ADDResultModel>(resultStr);
24                         if (!string.IsNullOrEmpty(result.error_description))
25                         {
26                             context.Response.Write(result.error_description);
27                         }
28                         JsonWebTokenHandler handler = new JsonWebTokenHandler();
29                         if (!string.IsNullOrEmpty(result.access_token) && handler.CanReadToken(result.access_token))
30                         {
31                             JsonWebToken jwt = handler.ReadJsonWebToken(result.access_token);
32                             var claims = jwt.Claims;
33                             Claim claim1 = claims.Where(i => i.Type == "oid").FirstOrDefault();
34                             Claim claim2 = claims.Where(i => i.Type == "unique_name").FirstOrDefault();
35                             Console.WriteLine(claim1.Value);
36                             Console.WriteLine(claim2.Value);
37                         }
38                         else if (!string.IsNullOrEmpty(result.id_token) && handler.CanReadToken(result.id_token))
39                         {
40                             JsonWebToken jwt = handler.ReadJsonWebToken(result.id_token);
41                             var claims = jwt.Claims;
42                             Claim claim1 = claims.Where(i => i.Type == "oid").FirstOrDefault();
43                             Claim claim2 = claims.Where(i => i.Type == "preferred_username").FirstOrDefault();
44                             Console.WriteLine(claim1.Value);
45                             Console.WriteLine(claim2.Value);
46                         }
47                         else
48                             context.Response.Write("token Exception!");
49                     }
50                 }
51             }
52             catch (Exception ex)
53             {
54                 context.Response.Write(ex.Message);
55             }

 

posted on 2021-06-24 17:18  诗情寻知己  阅读(131)  评论(0)    收藏  举报