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);
        }
Esempio n. 2
0
 public BasicAuthenticationMiddleware(RequestDelegate next, BasicAuthOptions options)
 {
     _next        = next;
     this.options = options;
 }