builder.Services.AddOpenIddict()
.AddCore(opt => { })
.AddServer(opt =>
{
opt.AllowAuthorizationCodeFlow()
.AllowRefreshTokenFlow();
opt.SetIssuer(new Uri("http://"));
opt.SetAuthorizationEndpointUris("oauth2/authorize")
.SetTokenEndpointUris("oauth2/token")
.SetIntrospectionEndpointUris("oauth2/introspect")
.SetConfigurationEndpointUris("oauth2/.well-known/openid-configuration")
.SetCryptographyEndpointUris("oauth2/.well-known/jwks");
opt.AddEphemeralEncryptionKey();
opt.AddEphemeralSigningKey();
opt.EnableDegradedMode();
opt.UseAspNetCore()
.DisableTransportSecurityRequirement();
opt.DisableAccessTokenEncryption();
opt.RequireProofKeyForCodeExchange();
// no use in degraded mode
opt.UseReferenceAccessTokens()
.UseReferenceRefreshTokens();
opt.AddEventHandler(x =>
{
x.UseInlineHandler(c =>
{
return default;
});
});
opt.AddEventHandler(x =>
{
// validate client, e.g., client_id, redirect_url
x.UseScopedHandler();
});
opt.AddEventHandler(x =>
{
x.UseScopedHandler();
});
opt.AddEventHandler(x =>
{
x.UseInlineHandler(c =>
{
return default;
});
});
public class ValidateAuthorizationRequestContext : IOpenIddictServerHandler
{
public ValueTask HandleAsync(OpenIddictServerEvents.ValidateAuthorizationRequestContext context)
{
if (!String.Equals(context.ClientId, "test", StringComparison.Ordinal))
{
context.Reject(
error: Errors.InvalidClient,
description: "The specified 'client_id' doesn't match a registered application."
);
}
return default;
}
}
public class HandleAuthorizationRequestContext : IOpenIddictServerHandler
{
public ValueTask HandleAsync(OpenIddictServerEvents.HandleAuthorizationRequestContext context)
{
// play with the ClaimsPrincipal
var identity = new ClaimsIdentity(TokenValidationParameters.DefaultAuthenticationType);
var claim = new Claim(Claims.Subject, "testSubject");
claim.SetDestinations(Destinations.AccessToken);
identity.AddClaim(claim);
//context.Principal = new ClaimsPrincipal(identity);
context.SignIn(new ClaimsPrincipal(identity));
return default;
}
}
При использовании клиента OAuth2 для входа в систему я обнаружил обратный вызов для ProcessAuthenticationContext (я не знаю, для чего нужен этот обратный вызов), ValidateAuthorizationRequestContext ( проверьте, действителен ли client_id) и HandleAuthorizationRequestContext (установите sub и другие утверждения в токене) будут вызываться последовательно. Но во всех этих трех обратных вызовах я не вижу никаких возможностей отобразить страницу входа в систему, чтобы пользователь мог ввести свое имя пользователя и пароль. Как лучше всего это сделать?
Я настраиваю новое приложение ASP.Net Core с ухудшенным режимом OpenIdDict в качестве сервера OAuth2. Я инициализировал сервер с помощью [code]builder.Services.AddOpenIddict() .AddCore(opt => { }) .AddServer(opt => { opt.AllowAuthorizationCodeFlow() .AllowRefreshTokenFlow();
public class ValidateAuthorizationRequestContext : IOpenIddictServerHandler { public ValueTask HandleAsync(OpenIddictServerEvents.ValidateAuthorizationRequestContext context) { if (!String.Equals(context.ClientId, "test", StringComparison.Ordinal)) { context.Reject( error: Errors.InvalidClient, description: "The specified 'client_id' doesn't match a registered application." ); } return default; } }
public class HandleAuthorizationRequestContext : IOpenIddictServerHandler { public ValueTask HandleAsync(OpenIddictServerEvents.HandleAuthorizationRequestContext context) { // play with the ClaimsPrincipal var identity = new ClaimsIdentity(TokenValidationParameters.DefaultAuthenticationType); var claim = new Claim(Claims.Subject, "testSubject"); claim.SetDestinations(Destinations.AccessToken); identity.AddClaim(claim); //context.Principal = new ClaimsPrincipal(identity); context.SignIn(new ClaimsPrincipal(identity)); return default; } } [/code] При использовании клиента OAuth2 для входа в систему я обнаружил обратный вызов для ProcessAuthenticationContext (я не знаю, для чего нужен этот обратный вызов), ValidateAuthorizationRequestContext ( проверьте, действителен ли client_id) и HandleAuthorizationRequestContext (установите sub и другие утверждения в токене) будут вызываться последовательно. Но во всех этих трех обратных вызовах я не вижу никаких возможностей отобразить страницу входа в систему, чтобы пользователь мог ввести свое имя пользователя и пароль. Как лучше всего это сделать?