1
1
using Google . Apis . Auth . OAuth2 ;
2
2
using Google . Apis . Auth . OAuth2 . Requests ;
3
3
using Google . Apis . Auth . OAuth2 . Responses ;
4
- using Microsoft . AspNetCore . Http . Extensions ;
5
4
6
5
namespace GoogleOIDC_Angular_ASPNETWebAPI_Auth_Code_Flow . Services ;
7
6
@@ -10,17 +9,24 @@ public class OIDCService
10
9
private readonly IHttpClientFactory _httpClientFactory ;
11
10
private readonly IConfiguration _config ;
12
11
13
- private string RedirectUri { get ; set ; } = "http://localhost:7091/Auth/oidc/signin" ;
12
+ private string RedirectUri { get ; set ; } = "http://localhost:7091/api/ Auth/oidc/signin" ;
14
13
private string ClientId { get ; }
15
14
private string ClientSecret { get ; }
16
15
17
- public OIDCService ( IHttpClientFactory httpClientFactory , IConfiguration config )
16
+ public OIDCService ( IHttpClientFactory httpClientFactory ,
17
+ IConfiguration config ,
18
+ string requestUrl )
18
19
{
19
20
_httpClientFactory = httpClientFactory ;
20
21
_config = config ;
21
22
23
+ if ( ! string . IsNullOrEmpty ( requestUrl ) )
24
+ SetupRedirectUri ( requestUrl ) ;
25
+
22
26
// Read redirectUri from configuration if it exists.
23
- RedirectUri = config [ "Authentication:Google:RedirectUri" ] ?? RedirectUri ;
27
+ RedirectUri = ! string . IsNullOrEmpty ( _config [ "Authentication:Google:RedirectUri" ] )
28
+ ? _config [ "Authentication:Google:RedirectUri" ]
29
+ : RedirectUri ;
24
30
ClientId = _config [ "Authentication:Google:ClientId" ] ;
25
31
ClientSecret = _config [ "Authentication:Google:ClientSecret" ] ;
26
32
}
@@ -52,13 +58,13 @@ public async Task<string> GetIdTokenAsync(string authorization_code)
52
58
53
59
#region Additional Feature
54
60
/// <summary>
55
- /// 由Request動態取得request URL,以產生並覆寫RedirectUri。在一進入Action時呼叫。
61
+ /// 動態取得request URL,以產生並覆寫RedirectUri
56
62
/// </summary>
57
63
/// <param name="request"></param>
58
64
/// <param name="route"></param>
59
- internal void SetupRedirectUri ( HttpRequest request , string route )
65
+ internal void SetupRedirectUri ( string requestUrl , string ? route = "api/Auth/oidc/signin" )
60
66
{
61
- Uri uri = new ( request . GetDisplayUrl ( ) ) ;
67
+ Uri uri = new ( requestUrl ) ;
62
68
string port = uri . Scheme == "https" && uri . Port == 443
63
69
|| uri . Scheme == "http" && uri . Port == 80
64
70
? ""
0 commit comments