78 lines
2.5 KiB
C#
78 lines
2.5 KiB
C#
using BaronSoftware;
|
|
using BaronSoftware.Auth;
|
|
using BaronSoftware.SSO;
|
|
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace BaronSoftware.Auth.Sample
|
|
{
|
|
/// <summary>
|
|
/// 실행 파일과 같은 폴더의 appsettings.json에서 접속 정보를 읽는다.
|
|
/// 파일이 없으면 내장 기본값을 사용하고, JSON 형식 오류는 예외로 알린다.
|
|
/// </summary>
|
|
public sealed class SampleSettings
|
|
{
|
|
public const string FileName = "appsettings.json";
|
|
|
|
public OidcSection Oidc { get; set; } = new();
|
|
|
|
public sealed class OidcSection
|
|
{
|
|
public string Authority { get; set; }
|
|
public string ClientId { get; set; }
|
|
public string RedirectUri { get; set; }
|
|
public string LogoutUri { get; set; }
|
|
public string Scope { get; set; }
|
|
}
|
|
|
|
private static readonly JsonSerializerOptions JsonOptions = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
ReadCommentHandling = JsonCommentHandling.Skip,
|
|
AllowTrailingCommas = true,
|
|
};
|
|
|
|
/// <summary>실행 파일 옆 appsettings.json을 로드. 파일이 없으면 기본값.</summary>
|
|
public static SampleSettings Load()
|
|
{
|
|
var path = Path.Combine(AppContext.BaseDirectory, FileName);
|
|
if (!File.Exists(path))
|
|
return new SampleSettings();
|
|
|
|
try
|
|
{
|
|
var json = File.ReadAllText(path);
|
|
return JsonSerializer.Deserialize<SampleSettings>(json, JsonOptions) ?? new SampleSettings();
|
|
}
|
|
catch (JsonException ex)
|
|
{
|
|
throw new InvalidOperationException(
|
|
$"설정 파일({FileName}) 형식 오류: {ex.Message}", ex);
|
|
}
|
|
}
|
|
|
|
|
|
private static readonly JsonSerializerOptions SaveOptions = new()
|
|
{
|
|
WriteIndented = true,
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
|
};
|
|
|
|
/// <summary>현재 설정을 실행 파일 옆 appsettings.json에 저장한다.</summary>
|
|
public void Save()
|
|
{
|
|
var path = Path.Combine(AppContext.BaseDirectory, FileName);
|
|
File.WriteAllText(path, JsonSerializer.Serialize(this, SaveOptions));
|
|
}
|
|
|
|
public BaronSSOOption ToOidcOptions() => new()
|
|
{
|
|
Authority = Oidc.Authority,
|
|
ClientId = Oidc.ClientId,
|
|
RedirectUri = Oidc.RedirectUri,
|
|
};
|
|
}
|
|
}
|