Able to connect with the auth endpoint for oidc

This commit is contained in:
LittleSheep 2025-06-16 23:42:55 +08:00
parent fe04b12561
commit 60a8338c9a
2 changed files with 21 additions and 9 deletions

View File

@ -180,8 +180,6 @@ public class ConnectionController(
var accountId = Guid.Parse(stateParts[0]);
return await HandleManualConnection(provider, oidcService, callbackData, accountId);
// Otherwise, it's a login or registration flow.
}
private async Task<IActionResult> HandleManualConnection(string provider, OidcService oidcService,

View File

@ -21,17 +21,31 @@ public class OidcController(
{
try
{
// Get the appropriate provider service
var oidcService = GetOidcService(provider);
// Generate state (containing return URL) and nonce
// If user is already authenticated, treat as an account connection request
if (HttpContext.Items["CurrentUser"] is Account.Account currentUser)
{
var state = Guid.NewGuid().ToString();
var nonce = Guid.NewGuid().ToString();
// Store user's ID, provider, and nonce in session. The callback will use this.
HttpContext.Session.SetString($"oidc_state_{state}", $"{currentUser.Id}|{provider}|{nonce}");
// The state parameter sent to the provider is the GUID key for the session state.
var authUrl = oidcService.GetAuthorizationUrl(state, nonce);
return Redirect(authUrl);
}
else // Otherwise, proceed with login/registration flow
{
var state = returnUrl;
var nonce = Guid.NewGuid().ToString();
// Get the authorization URL and redirect the user
// The state parameter is the returnUrl. The callback will not find a session state and will treat it as a login.
var authUrl = oidcService.GetAuthorizationUrl(state ?? "/", nonce);
return Redirect(authUrl);
}
}
catch (Exception ex)
{
return BadRequest($"Error initiating OpenID Connect flow: {ex.Message}");