fix "Cannot generate SSPI context" error and fix SqlException: Failed
to generate SSPI context.
try to
download Microsoft® Kerberos Configuration Manager
download link
in query string try to write Encrypt=False;
query string Server=ip_or_your_servername,your_port_or_1433\SQLEXPRESS;database=Your_database_Name;user=your_username;password=your_password;TrustServerCertificate=True;Encrypt=False;"
for SQL Server® and install in your server.
Microsoft® Kerberos Configuration Manager download link
Kerberos authentication provides a highly secure method to authenticate client and server entities (security principals) on a network. To use Kerberos authentication with SQL Server, a Service Principal Name (SPN) must be registered with Active Directory, which plays the role of the Key Distribution Center in a Windows domain. In addition, many customers also enable delegation for multi-tier applications using SQL Server. In such a setup, it may be difficult to troubleshoot the connectivity problems with SQL Server when Kerberos authentication fails.
The Kerberos Configuration Manager for SQL Server is a diagnostic tool that helps troubleshoot Kerberos related connectivity issues with SQL Server, SQL Server Reporting Services, and SQL Server Analysis Services. It can perform the following functions:
Gather information on OS, Microsoft SQL Server instances and Always On Availability Group Listeners installed on a server.
Report on all SPN and delegation configurations on the server.
Identify potential problems in SPNs and delegations.
Fix potential SPN problems.
Microsoft Kerberos Configuration Manager for SQL Server is a diagnostic tool that helps troubleshoot Kerberos related connectivity issues with SQL Server, SQL Server Reporting Services, and SQL Server Analysis Services.

Installer will install in this path. if you want to install to a different folder, use Browse.
C:\Program Files\Microsoft\Kerberos Configuration Manager for SQL Server\
then try to config SPN
For example, a typical SPN for a server that is running an instance of SQL Server is as follows:
MSSQLSvc/SQLSERVER1.northamerica.corp.mycompany.com:1433
https://learn.microsoft.com/en-US/troubleshoot/sql/database-engine/connect/cannot-generate-sspi-context-error#see-also