FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ConvertLearnToDoc/src/ConvertLearnToDoc/Program.cs at main · markjulmar/ConvertLearnToDoc · GitHub
markjulmar
/
ConvertLearnToDoc
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
Pull requests
8
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ConvertLearnToDoc
/
src
/
ConvertLearnToDoc
/
Program.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
234 lines (202 loc) · 8.51 KB
Breadcrumbs
ConvertLearnToDoc
/
src
/
ConvertLearnToDoc
/
Program.cs
Copy path
File metadata and controls
234 lines (202 loc) · 8.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using
ConvertLearnToDoc
.
Utility
;
using
Microsoft
.
AspNetCore
.
Authentication
;
using
Microsoft
.
AspNetCore
.
Authentication
.
Cookies
;
#if
USE_MS_AUTH
using
Microsoft
.
AspNetCore
.
Authentication
.
MicrosoftAccount
;
#else
using
Microsoft
.
AspNetCore
.
Authentication
.
OAuth
;
#endif
using
Microsoft
.
AspNetCore
.
Components
.
Authorization
;
using
Microsoft
.
AspNetCore
.
Components
.
Server
;
using
System
.
Security
.
Claims
;
using
System
.
Net
.
Http
.
Headers
;
using
System
.
Text
.
Json
;
#if
!
DEBUG
using
Microsoft
.
Extensions
.
Logging
.
ApplicationInsights
;
#endif
var
builder
=
WebApplication
.
CreateBuilder
(
args
)
;
// Add services to the container.
builder
.
Services
.
AddRazorPages
(
)
;
builder
.
Services
.
AddServerSideBlazor
(
)
;
builder
.
Services
.
AddScoped
<
AuthenticationStateProvider
,
ServerAuthenticationStateProvider
>
(
)
;
#if
!
DEBUG
builder
.
Services
.
AddApplicationInsightsTelemetry
(
)
;
#endif
builder
.
Services
.
AddHttpClient
(
)
;
builder
.
Services
.
AddHttpContextAccessor
(
)
;
#if
!
USE_MS_AUTH
builder
.
Services
.
AddScoped
<
TokenService
>
(
)
;
#endif
builder
.
Logging
.
SetMinimumLevel
(
LogLevel
.
Debug
)
;
builder
.
Logging
.
AddConsole
(
)
;
#if
!
DEBUG
builder
.
Logging
.
AddApplicationInsights
(
)
;
builder
.
Logging
.
AddFilter
<
ApplicationInsightsLoggerProvider
>
(
""
,
LogLevel
.
Warning
)
;
#endif
#if
USE_MS_AUTH
builder
.
Services
.
AddAuthentication
(
CookieAuthenticationDefaults
.
AuthenticationScheme
)
.
AddCookie
(
)
.
AddMicrosoftAccount
(
ms
=>
{
ms
.
ClientId
=
builder
.
Configuration
[
"AzureApp:ClientId"
]
!
;
ms
.
ClientSecret
=
builder
.
Configuration
[
"AzureApp:SecretKey"
]
!
;
ms
.
SaveTokens
=
true
;
}
)
;
#else
var
gitHubAppInfo
=
builder
.
Configuration
.
GetSection
(
"GitHub"
)
??
throw
new
Exception
(
"GitHub section is missing in the configuration file."
)
;
builder
.
Services
.
AddAuthentication
(
options
=>
{
options
.
DefaultAuthenticateScheme
=
CookieAuthenticationDefaults
.
AuthenticationScheme
;
options
.
DefaultSignInScheme
=
CookieAuthenticationDefaults
.
AuthenticationScheme
;
options
.
DefaultChallengeScheme
=
"GitHub"
;
}
)
.
AddCookie
(
options
=>
{
options
.
ExpireTimeSpan
=
TimeSpan
.
FromMinutes
(
60
)
;
// Default cookie expiration time
options
.
Events
=
new
CookieAuthenticationEvents
{
OnSigningIn
=
context
=>
{
var
expiresAt
=
context
.
Properties
.
GetTokenValue
(
"expires_at"
)
;
if
(
DateTime
.
TryParse
(
expiresAt
,
out
var
expiry
)
)
{
context
.
Properties
.
ExpiresUtc
=
expiry
;
context
.
CookieOptions
.
Expires
=
expiry
;
}
return
Task
.
CompletedTask
;
}
}
;
}
)
.
AddOAuth
(
"GitHub"
,
options
=>
{
options
.
ClientId
=
gitHubAppInfo
[
"ClientId"
]
??
throw
new
Exception
(
"GitHub section missing ClientId."
)
;
options
.
ClientSecret
=
gitHubAppInfo
[
"ClientSecret"
]
??
throw
new
Exception
(
"GitHub section missing ClientSecret."
)
;
options
.
CallbackPath
=
PathString
.
FromUriComponent
(
new
Uri
(
gitHubAppInfo
[
"CallbackUrl"
]
??
throw
new
Exception
(
"GitHub section missing CallbackUrl."
)
)
)
;
options
.
AuthorizationEndpoint
=
"https://github.com/login/oauth/authorize"
;
options
.
TokenEndpoint
=
"https://github.com/login/oauth/access_token"
;
options
.
UserInformationEndpoint
=
"https://api.github.com/user"
;
options
.
Scope
.
Add
(
"read:user"
)
;
options
.
Scope
.
Add
(
"user:email"
)
;
// Require email
#if
USE_GITHUB_PAT
options
.
Scope
.
Add
(
"repo"
)
;
options
.
Scope
.
Add
(
"read:org"
)
;
#endif
options
.
ClaimActions
.
MapJsonKey
(
ClaimTypes
.
NameIdentifier
,
"id"
)
;
options
.
ClaimActions
.
MapJsonKey
(
ClaimTypes
.
Name
,
"name"
)
;
options
.
ClaimActions
.
MapJsonKey
(
ClaimTypes
.
Email
,
"email"
)
;
options
.
SaveTokens
=
true
;
options
.
Events
=
new
OAuthEvents
{
OnCreatingTicket
=
async
context
=>
{
var
request
=
new
HttpRequestMessage
(
HttpMethod
.
Get
,
context
.
Options
.
UserInformationEndpoint
)
;
request
.
Headers
.
Accept
.
Add
(
new
MediaTypeWithQualityHeaderValue
(
"application/json"
)
)
;
request
.
Headers
.
Authorization
=
new
AuthenticationHeaderValue
(
"Bearer"
,
context
.
AccessToken
)
;
var
response
=
await
context
.
Backchannel
.
SendAsync
(
request
,
HttpCompletionOption
.
ResponseHeadersRead
,
context
.
HttpContext
.
RequestAborted
)
;
response
.
EnsureSuccessStatusCode
(
)
;
var
user
=
JsonDocument
.
Parse
(
await
response
.
Content
.
ReadAsStringAsync
(
)
)
;
context
.
RunClaimActions
(
user
.
RootElement
)
;
// Fetch the email if it's not provided
if
(
!
user
.
RootElement
.
TryGetProperty
(
"email"
,
out
var
emailInfo
)
||
string
.
IsNullOrEmpty
(
emailInfo
.
GetString
(
)
)
)
{
var
emailRequest
=
new
HttpRequestMessage
(
HttpMethod
.
Get
,
"https://api.github.com/user/emails"
)
;
emailRequest
.
Headers
.
Accept
.
Add
(
new
MediaTypeWithQualityHeaderValue
(
"application/json"
)
)
;
emailRequest
.
Headers
.
Authorization
=
new
AuthenticationHeaderValue
(
"Bearer"
,
context
.
AccessToken
)
;
var
emailResponse
=
await
context
.
Backchannel
.
SendAsync
(
emailRequest
,
HttpCompletionOption
.
ResponseHeadersRead
,
context
.
HttpContext
.
RequestAborted
)
;
emailResponse
.
EnsureSuccessStatusCode
(
)
;
var
emails
=
JsonDocument
.
Parse
(
await
emailResponse
.
Content
.
ReadAsStringAsync
(
)
)
;
var
primaryEmail
=
emails
.
RootElement
.
EnumerateArray
(
)
.
FirstOrDefault
(
e
=>
e
.
GetProperty
(
"primary"
)
.
GetBoolean
(
)
)
.
GetProperty
(
"email"
)
.
GetString
(
)
;
if
(
!
string
.
IsNullOrEmpty
(
primaryEmail
)
)
{
context
.
Identity
!
.
AddClaim
(
new
Claim
(
ClaimTypes
.
Email
,
primaryEmail
)
)
;
}
}
// Save the access token
if
(
context
.
AccessToken
!=
null
)
{
context
.
Identity
!
.
AddClaim
(
new
Claim
(
"access_token"
,
context
.
AccessToken
)
)
;
var
expiresAt
=
DateTime
.
UtcNow
.
AddSeconds
(
int
.
Parse
(
context
.
TokenResponse
.
ExpiresIn
??
"3600"
)
)
;
context
.
Properties
.
StoreTokens
(
[
new
AuthenticationToken
{
Name
=
"access_token"
,
Value
=
context
.
AccessToken
}
,
new
AuthenticationToken
{
Name
=
"expires_at"
,
Value
=
expiresAt
.
ToString
(
"o"
)
}
]
)
;
context
.
Properties
.
ExpiresUtc
=
expiresAt
;
}
}
}
;
}
)
;
#endif
var
app
=
builder
.
Build
(
)
;
// Configure the HTTP request pipeline.
if
(
!
app
.
Environment
.
IsDevelopment
(
)
)
{
app
.
UseExceptionHandler
(
"/Error"
)
;
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app
.
UseHsts
(
)
;
}
app
.
UseHttpsRedirection
(
)
;
app
.
UseStaticFiles
(
)
;
app
.
UseRouting
(
)
;
/*
app.UseCookiePolicy(new CookiePolicyOptions
{
MinimumSameSitePolicy = SameSiteMode.None
});
*/
app
.
UseAuthentication
(
)
;
app
.
UseAuthorization
(
)
;
app
.
MapPost
(
"/signout"
,
async
ctx
=>
{
await
ctx
.
SignOutAsync
(
new
AuthenticationProperties
{
RedirectUri
=
"/"
}
)
;
}
)
;
app
.
MapGet
(
"/signin"
,
async
ctx
=>
{
#if
USE_MS_AUTH
await
ctx
.
ChallengeAsync
(
MicrosoftAccountDefaults
.
AuthenticationScheme
,
new
AuthenticationProperties
{
RedirectUri
=
"/signin-callback"
}
)
;
#else
await
ctx
.
ChallengeAsync
(
"GitHub"
,
new
AuthenticationProperties
{
RedirectUri
=
"/signin-callback"
}
)
;
#endif
}
)
;
app
.
MapGet
(
"/signin-callback"
,
async
ctx
=>
{
var
result
=
await
ctx
.
AuthenticateAsync
(
)
;
if
(
result
.
Succeeded
)
{
var
user
=
result
.
Principal
;
var
email
=
user
?
.
Claims
.
FirstOrDefault
(
c
=>
c
.
Type
==
ClaimTypes
.
Email
)
?
.
Value
;
bool
isValid
=
ControllerExtensions
.
ValidateUser
(
email
)
;
if
(
isValid
&&
user
?
.
Identity
?
.
IsAuthenticated
==
true
&&
!
string
.
IsNullOrEmpty
(
email
)
)
{
//var claimsIdentity = new ClaimsIdentity(new[] {
// new Claim(ClaimTypes.Name, user.Identity.Name ?? "User"),
// new Claim(ClaimTypes.Email, email)
//}, CookieAuthenticationDefaults.AuthenticationScheme);
//var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
await
ctx
.
SignInAsync
(
CookieAuthenticationDefaults
.
AuthenticationScheme
,
user
)
;
ctx
.
Response
.
Redirect
(
"/"
)
;
}
else
{
await
ctx
.
SignOutAsync
(
CookieAuthenticationDefaults
.
AuthenticationScheme
,
new
AuthenticationProperties
{
RedirectUri
=
"/"
}
)
;
}
}
else
{
await
ctx
.
SignOutAsync
(
CookieAuthenticationDefaults
.
AuthenticationScheme
,
new
AuthenticationProperties
{
RedirectUri
=
"/"
}
)
;
}
}
)
;
app
.
MapControllers
(
)
;
app
.
MapBlazorHub
(
)
;
app
.
MapFallbackToPage
(
"/_Host"
)
;
app
.
Run
(
)
;
Back
|
FazBrowse Home
|
New Git URL