Setup SelftSigningCertificate x509 in asp.net core blazor wasm

Prathamesh Shende 376 Reputation points
2023-08-26T17:14:16.0266667+00:00

How to setup x509 self signed certificate in asp.net core 7.0 with blazor wasm

here is my program.cs server project asp.net core
I tried to go through docs of Microsoft but didn't understand what code and where to put.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSignalR();
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false)
    .AddEntityFrameworkStores<ApplicationDbContext>();

builder.Services.AddIdentityServer()
    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

builder.Services.AddAuthentication()
    .AddIdentityServerJwt();
builder.Services.Configure<GzipCompressionProviderOptions>(options =>
{
    options.Level = CompressionLevel.Fastest;
});
builder.Services.AddResponseCompression(option =>
{
    option.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "application/octet-stream" });
});
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
    app.UseWebAssemblyDebugging();
}
else
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseResponseCompression();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();

app.UseIdentityServer();
app.UseAuthorization();


app.MapRazorPages();
app.MapControllers();

app.MapFallbackToFile("index.html");

app.Run();

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,403 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,500 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Syed Shah Hussain Bukhari 140 Reputation points
    2023-08-27T00:58:17.6733333+00:00
    Setting up a self-signed X509 certificate in an ASP.NET Core 7.0 project with Blazor WebAssembly involves a few steps. Let me guide you through the process.
    
    1. First, you need to generate a self-signed certificate. You can use the `openssl` command-line tool or a tool like the Windows Certificate Manager to generate the certificate.
    
    2. Once you have the certificate, save it to a specific location on your server project. For example, you can create a folder named "Certificates" in your project's root directory and save the certificate there.
    
    3. Open the `Program.cs` file in your server project. This file is typically located in the root folder of your ASP.NET Core project.
    
    4. In the `Program.cs` file, add the following code to configure the HTTPS options:
    
    ```csharp
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Hosting;
    using System.Net;
    
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }
    
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    webBuilder.UseKestrel(options =>
                    {
                        options.Listen(IPAddress.Any, 5000); // Change the port number to the one you want to use
                        options.Listen(IPAddress.Any, 5001, listenOptions =>
                        {
                            listenOptions.UseHttps("<path to your certificate.pfx>", "<certificate password>");
                        });
                    });
                });
    }
    

    Make sure to replace <path to your certificate.pfx> with the actual path to your self-signed certificate file. Also, replace <certificate password> with the password you used when generating the certificate.

    1. Save the Program.cs file.

    After completing these steps, your ASP.NET Core server project should be configured to use the self-signed X509 certificate for HTTPS. Remember to replace the port number and certificate path and password with your actual values.

    I hope this helps you configure the X509 self-signed certificate in your ASP.NET Core 7.0 project with Blazor WebAssembly. Let me know if you have any further questions!