public static void AddBasicAuthentication(this IServiceCollection services, IConfiguration configuration) { var basicAuthOptions = new BasicAuthOptions(); configuration.Bind(basicAuthOptions); if (string.IsNullOrEmpty(basicAuthOptions.Username) && string.IsNullOrEmpty(basicAuthOptions.Password)) { byte[] data = new byte[20]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); rng.GetBytes(data); basicAuthOptions.Username = Convert.ToBase64String(data); rng.GetBytes(data); basicAuthOptions.Password = Convert.ToBase64String(data); Console.WriteLine($"Using Basic Auth - Username: \"{basicAuthOptions.Username}\" Password: \"{basicAuthOptions.Password}\""); } services.AddSingleton(basicAuthOptions); }
public BasicAuthenticationMiddleware(RequestDelegate next, BasicAuthOptions options) { _next = next; this.options = options; }