2020-12-21 22:58:40 +02:00
|
|
|
|
using IdentityServer.Wrapper;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-01-12 01:01:36 +02:00
|
|
|
|
using Netmash.Security.Authentication.Identity.Abstractions;
|
2020-12-21 22:58:40 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
2023-01-12 01:01:36 +02:00
|
|
|
|
namespace Netmash.Security.Authentication.Identity
|
2020-12-21 22:58:40 +02:00
|
|
|
|
{
|
2021-06-26 21:31:31 +03:00
|
|
|
|
public static class AuthenticationExtensions
|
2020-12-21 22:58:40 +02:00
|
|
|
|
{
|
2021-11-13 23:52:26 +02:00
|
|
|
|
public static IServiceCollection AddIdentityAuthentication(this IServiceCollection services, string identityServerBaseAddress)
|
2020-12-21 22:58:40 +02:00
|
|
|
|
{
|
2021-11-24 03:40:30 +02:00
|
|
|
|
services.AddIdentityAuthentication(identityServerBaseAddress, new Models.AuthenticationOptions());
|
2021-06-26 21:20:49 +03:00
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-13 23:52:26 +02:00
|
|
|
|
public static IServiceCollection AddIdentityAuthentication(this IServiceCollection services, string identityServerBaseAddress, IAuthenticationOptions authenticationOptions)
|
2021-06-26 21:20:49 +03:00
|
|
|
|
{
|
|
|
|
|
Validate(identityServerBaseAddress, authenticationOptions);
|
2020-12-21 22:58:40 +02:00
|
|
|
|
|
|
|
|
|
// Identity server
|
|
|
|
|
services.UseIdentityServices(identityServerBaseAddress);
|
2021-06-26 21:20:49 +03:00
|
|
|
|
services.AddSingleton(authenticationOptions);
|
2020-12-21 22:58:40 +02:00
|
|
|
|
|
2021-06-26 21:31:31 +03:00
|
|
|
|
// configure authentication
|
2021-11-13 23:52:26 +02:00
|
|
|
|
services.AddAuthentication("IdentityAuthentication")
|
2021-06-26 21:31:31 +03:00
|
|
|
|
.AddScheme<AuthenticationSchemeOptions, IdentityAuthenticationHandler>("IdentityAuthentication", null);
|
2020-12-21 22:58:40 +02:00
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
2021-06-26 21:20:49 +03:00
|
|
|
|
|
|
|
|
|
private static void Validate(string identityServerBaseAddress, IAuthenticationOptions authenticationOptions)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(identityServerBaseAddress))
|
|
|
|
|
throw new ArgumentException("Identity server base address must be provided.");
|
|
|
|
|
|
|
|
|
|
var guestFuncDefined = authenticationOptions.AuthenticateAsGuest != null;
|
|
|
|
|
if (guestFuncDefined && string.IsNullOrEmpty(authenticationOptions.GuestUserName))
|
|
|
|
|
throw new ArgumentException("Guest function is defined, but guest user name is not set.");
|
|
|
|
|
}
|
2020-12-21 22:58:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|