|
|
@ -1,4 +1,3 @@
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
using System.Security.Claims;
|
|
|
|
using System.Text.Json;
|
|
|
|
using System.Text.Json;
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
@ -26,24 +25,20 @@ builder.Services.AddAuthentication(options => {
|
|
|
|
options.CallbackPath = authConfig["CallbackPath"]!;
|
|
|
|
options.CallbackPath = authConfig["CallbackPath"]!;
|
|
|
|
options.AuthorizationEndpoint = authConfig["AuthorizationEndpoint"]!;
|
|
|
|
options.AuthorizationEndpoint = authConfig["AuthorizationEndpoint"]!;
|
|
|
|
options.TokenEndpoint = authConfig["TokenEndpoint"]!;
|
|
|
|
options.TokenEndpoint = authConfig["TokenEndpoint"]!;
|
|
|
|
options.UserInformationEndpoint = authConfig["UserInformationEndpoint"]!;
|
|
|
|
|
|
|
|
options.SignInScheme = "Cookie";
|
|
|
|
options.SignInScheme = "Cookie";
|
|
|
|
options.Backchannel = new HttpClient(new OriginHandler("http://localhost:5255"));
|
|
|
|
options.Backchannel = new HttpClient(new OriginHandler("http://localhost:5255"));
|
|
|
|
|
|
|
|
|
|
|
|
options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "userId");
|
|
|
|
options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "sub");
|
|
|
|
|
|
|
|
|
|
|
|
options.Events = new OAuthEvents {
|
|
|
|
options.Events = new OAuthEvents {
|
|
|
|
OnCreatingTicket = async context => {
|
|
|
|
OnCreatingTicket = context => {
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
|
|
|
|
var payloadBase64 = context.AccessToken!.Split('.')[1];
|
|
|
|
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
|
|
var payloadJson = Base64UrlTextEncoder.Decode(payloadBase64);
|
|
|
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
|
|
|
|
var payload = JsonDocument.Parse(payloadJson);
|
|
|
|
|
|
|
|
|
|
|
|
var response = await context.Backchannel.SendAsync(request);
|
|
|
|
context.RunClaimActions(payload.RootElement);
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
var user = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context.RunClaimActions(user.RootElement);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|