This commit is contained in:
2026-10-11 17:38:12 +03:30
parent bfd60e9bb3
commit e86b50163c
6 changed files with 312 additions and 44 deletions
+46 -34
View File
@@ -399,31 +399,56 @@ namespace xIds.DI
/// </summary>
/// <param name="services"></param>
/// <param name="configuration"></param>
/// <param name="addApiKeyAuthentication"></param>
public static void AddXIdentityServerAuthentication(
this IServiceCollection services,
IConfiguration configuration,
bool addApiKeyAuthentication = false
IConfiguration configuration
)
{
//
// Adding Requirements ...
services.AddXIdentityResourceConfiguration(configuration);
//
// Create Authentication Builder ...
// var authBuilder = services.AddAuthentication();
//
// Create Authentication Builder ...
var authBuilder = services.AddAuthentication();
//
// For Best Support we Have to Add ApiKey Authentication First ...
if (addApiKeyAuthentication)
var authBuilder = services.AddAuthentication(options =>
{
//
authBuilder
.AddScheme<AuthenticationSchemeOptions, XApiKeyAuthenticationHandler>(
XAuthenticationScheme.XApiKey.GetStringValue(),
options => { });
}
// Setting Default Authentication Schema ...
// For Handling Smart Schema Forwarder ...
options.DefaultScheme = XAuthentication.SMART_SCHEME;
options.DefaultChallengeScheme = XAuthentication.SMART_SCHEME;
options.DefaultAuthenticateScheme = XAuthentication.SMART_SCHEME;
})
.AddPolicyScheme(
authenticationScheme: XAuthentication.SMART_SCHEME,
displayName: XAuthentication.SMART_SCHEME,
configureOptions: options =>
{
//
// Configure Forward Default Selector ...
options.ForwardDefaultSelector = context =>
{
//
// Retrieve XApiKey Header Key ...
var apiKeyHeader = XHeader.ApiKey.GetStringValue();
//
// If Header Contains Key ...
if (context.Request.Headers.ContainsKey(apiKeyHeader))
{
return XAuthenticationScheme.XApiKey.GetStringValue();
}
//
// If not ...
return XAuthentication.IDENTITY_SERVER_LOCAL_API;
};
}
);
//
// Add JwtBearer by Smart Schema ForwardDefaultSelector ...
@@ -432,26 +457,7 @@ namespace xIds.DI
//
options.SaveToken = true;
options.RequireHttpsMetadata = false;
// options.ForwardDefault = XAuthentication.IDENTITY_SERVER_LOCAL_API;
//
// Selecting Authentication Schema Based On Header ...
options.ForwardDefaultSelector = context =>
{
//
// if Header Contains X-Api-Key use Scheme XApiKey ...
var apiKeyHeader = XHeader.ApiKey
.GetStringValue()
.ToNormalString();
if (context.Request.Headers.ContainsKey(apiKeyHeader))
{
return XAuthenticationScheme.XApiKey.GetStringValue();
}
//
// If Not, Use Local Api ...
return XAuthentication.IDENTITY_SERVER_LOCAL_API;
};
options.ForwardDefault = XAuthentication.IDENTITY_SERVER_LOCAL_API;
})
.AddLocalApi();
}
@@ -505,7 +511,13 @@ namespace xIds.DI
options.AddPolicy(IdentityServerConstants.LocalApi.PolicyName, policy =>
{
//
policy.AddAuthenticationSchemes(IdentityServerConstants.LocalApi.AuthenticationScheme);
policy.AddAuthenticationSchemes(
[
XAuthenticationScheme.XApiKey.GetStringValue(),
IdentityServerConstants.LocalApi.AuthenticationScheme
]);
//
policy.RequireAuthenticatedUser();
});
});