Skip to content

Commit

Permalink
using client cert
Browse files Browse the repository at this point in the history
  • Loading branch information
birojnayak committed Oct 13, 2023
1 parent 433314e commit c10d404
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static IHost CreateWebHostBuilder<TStartup>(string linuxSocketFilepath =


//only for test, don't use in production code
public static async Task<X509Certificate2> GetServiceCertificateAsync()
public static X509Certificate2 GetServiceCertificate()
{
string AspNetHttpsOid = "1.3.6.1.4.1.311.84.1.1";
X509Certificate2 foundCert = null;
Expand Down Expand Up @@ -77,8 +77,12 @@ public static async Task<X509Certificate2> GetServiceCertificateAsync()
cert.Dispose();
}
}

if (foundCert == null)
foundCert = await ServiceUtilHelper.GetServiceMacineCertFromServerAsync();
{
foundCert = ServiceUtilHelper.ClientCertificate;
}

return foundCert;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void WindowsAuth()

[WcfFact]
[OuterLoop]
private async Task BasicCertAsTransportAsync()
private void BasicCertAsTransport()
{
string testString = new string('a', 3000);
IHost host = ServiceHelper.CreateWebHostBuilder<StartupForUnixDomainSocketTransportCertificate>(UDS.GetUDSFilePath());
Expand All @@ -112,7 +112,7 @@ private async Task BasicCertAsTransportAsync()
Scheme = "net.uds",
Path = UDS.GetUDSFilePath()
};
var cert = await ServiceHelper.GetServiceCertificateAsync();
var cert = ServiceHelper.GetServiceCertificate();
var identity = new X509CertificateEndpointIdentity(cert);
var factory = new System.ServiceModel.ChannelFactory<IEchoService>(binding,
new System.ServiceModel.EndpointAddress(new Uri(uriBuilder.ToString()), identity));
Expand Down Expand Up @@ -254,16 +254,16 @@ public void Configure(IHost host)
};

builder.AddServiceEndpoint<EchoService, IEchoService>(udsBinding, "net.uds://" + GetUDSFilePath());
Action<CoreWCF.ServiceHostBase> serviceHost = async host => await ChangeHostBehaviorAsync(host);
Action<CoreWCF.ServiceHostBase> serviceHost = host => ChangeHostBehavior(host);
builder.ConfigureServiceHostBase<EchoService>(serviceHost);
});
}

public async Task ChangeHostBehaviorAsync(CoreWCF.ServiceHostBase host)
public void ChangeHostBehavior(CoreWCF.ServiceHostBase host)
{
var srvCredentials = host.Credentials;
//provide the certificate, here we are getting the default asp.net core default certificate, not recommended for prod workload.
srvCredentials.ServiceCertificate.Certificate = await ServiceHelper.GetServiceCertificateAsync();
srvCredentials.ServiceCertificate.Certificate = ServiceHelper.GetServiceCertificate();
srvCredentials.ClientCertificate.Authentication.CertificateValidationMode = CoreWCF.Security.X509CertificateValidationMode.None;
}
}
Expand Down

0 comments on commit c10d404

Please sign in to comment.