Authoriztion Code Flow

1.IdentityServer项目中新建一个Client

因为项目模板默认只添加了OpenId,Profile等IdentityResource。我们如果想获得其他的Scopes如图中的Email等,需进行以下添加:

 2. 创建MvcClient客户端,并配置StartUp.cs

 

3.获取AccessToken及其他信息:

启动IdentityServer服务器,启动MvcClient客户端。输入正确的账户密码后,身份认证将会成功,IdentityServer将会返回用户信息,且将AccessToken等一并带回:

 

可以看到除了基本信息之外,我们刚刚给他添加的email等Scope也成功被授权访问成功。

 4.访问Api资源:

首先我们需要一个访问Api的方法:

获取RefreshToken的方法:

 1 private async Task<string> RenewTokenAsync()
 2         {
 3             var client = new HttpClient();
 4             var disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000");
 5 
 6             if (disco.IsError)
 7             {
 8                 throw new Exception(disco.Error);
 9             }
10 
11             // Get OpenIdConnect中规定的RefreshToken类型
12             var refreshToken = await HttpContext.GetTokenAsync(OpenIdConnectParameterNames.RefreshToken);
13 
14             //从授权服务器中获取RefreshTokenId
15             var tokenResponse = await client.RequestRefreshTokenAsync(new RefreshTokenRequest
16             {
17                 //以下为Get RefreshTokenId必填的参数
18 
19                 Address = disco.TokenEndpoint,
20                 ClientId = "mvc client",
21                 ClientSecret = "mvc secret",
22                 Scope = "scope1 openid profile email phone address",
23                 GrantType = OpenIdConnectGrantTypes.RefreshToken,
24                 RefreshToken = refreshToken
25             });
26 
27             if (tokenResponse.IsError)
28             {
29                 throw new Exception(tokenResponse.Error);
30             }
31             else
32             {
33                 var expiresAt = DateTime.UtcNow + TimeSpan.FromSeconds(tokenResponse.ExpiresIn);
34 
35                 var tokens = new[]
36                 {
37                     new AuthenticationToken
38                     {
39                         Name = OpenIdConnectParameterNames.IdToken,
40                         Value = tokenResponse.IdentityToken
41                     },
42 
43                     new AuthenticationToken
44                     {
45                         Name = OpenIdConnectParameterNames.AccessToken,
46                         Value = tokenResponse.AccessToken
47                     },
48 
49                     new AuthenticationToken
50                     {
51                         Name = OpenIdConnectParameterNames.RefreshToken,
52                         Value = tokenResponse.RefreshToken
53                     },
54 
55                     new AuthenticationToken
56                     {
57                         Name = "expires_at",
58                         Value = expiresAt.ToString("O", CultureInfo.InvariantCulture)
59                     }
60                 };
61 
62                 //获取身份认证的结果 包含当前的Principal Properties
63                 var currentAuthenticationResult =
64                     await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
65 
66                 //将通过RefreshToken获取的新的Token存起来
67                 currentAuthenticationResult.Properties.StoreTokens(tokens);
68 
69                 //重新获取授权并登录
70                 await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, currentAuthenticationResult.Principal, currentAuthenticationResult.Properties);
71 
72                 return tokenResponse.AccessToken;
73             }
74         }

 成功访问到Api1的资源:

 等待我们设置的一分钟后,能获取到RefreshToken刷新获得Api1资源。

原文地址:https://www.cnblogs.com/Xieyiincuit/p/13832173.html