Share via

How to set up https binding with WebAdministration API

Paul Dundon 1 Reputation point
2022-01-17T18:20:55.687+00:00

I am using a VB.NET app and the WebAdministration API to create a website, and want to create an https binding. Is there a good example of how to do this in the documentation?

I am using the following code:

        Dim hash As String = "[my key hash]"
        Dim arHash() As Byte = BigInteger.Parse(hash, System.Globalization.NumberStyles.HexNumber).ToByteArray().Reverse().ToArray()
        website.Bindings.Add($"*:443:{host}.omnisislabs.co.uk", arHash, "Personal")

The certificate is stored in the LocalComputer Personal store.

When I call ServerManager.CommitChanges, I get the following error:

System.Runtime.InteropServices.COMException A specified logon session does not exist. It may already have been terminated

I'd also like to be able to set the Require Server Name Indication flag.

Windows development | Internet Information Services
0 comments No comments

2 answers

Sort by: Most helpful
  1. Paul Dundon 1 Reputation point
    2022-01-18T07:37:10.63+00:00

    The problem was in the certificate store parameter - this should be "My" rather than "Personal".

    The final code was:

            Dim hash As String = "[my hash]"
            Dim arHash() As Byte = BigInteger.Parse(hash, System.Globalization.NumberStyles.HexNumber).ToByteArray().Reverse().ToArray()
            Dim sslBinding As Binding = website.Bindings.Add($"*:443:{host}.omnisislabs.co.uk", arHash, "My")
    
            sslBinding.SetAttributeValue("sslFlags", 1) ' require SNI
    
    0 comments No comments

  2. Sam Wu-MSFT 7,571 Reputation points Microsoft External Staff
    2022-01-18T03:33:17.9+00:00

    @Paul Dundon

    I am using a VB.NET app and the WebAdministration API to create a website, and want to create an https binding. Is there a good example of how to do this in the documentation?

    You can refer to this link: Create an SSL Binding.

    Set oBinding = oIIS.Get("BindingElement").SpawnInstance_  
    oBinding.BindingInformation = "*:443:"  
    oBinding.Protocol = "https"      
    Set oSite = oIIS.Get("Site.Name='Default Web Site'")  
    arrBindings = oSite.Bindings      
    ReDim Preserve arrBindings(UBound(arrBindings) + 1)  
    Set arrBindings(UBound(arrBindings)) = oBinding      
    oSite.Bindings = arrBindings  
    Set oPath = oSite.Put_  
    

    System.Runtime.InteropServices.COMException A specified logon session does not exist. It may already have been terminated

    This is an SSL certificate problem, you can try the following methods to solve the problem:

    Open up certificates in MMC

    Step 1: Open up a Run window and type "mmc"

    Step 2: Click File > Add/Remove Snap In

    Step 3: Add > Certificates, Click OK

    Step 4: Choose "Computer Account", then "Local Computer" and proceed.

    Step 5: Hit OK

    Export Certificate in MMC

    Step 1: Open "Certificates"

    Step 2: Open the folder where your certificate is stored.

    Step 3: Right Click on Certificate, All Tasks, Export

    Step 4: Export to the server Desktop

    Now you should be able to re-import your certificate into IIS (or just into MMC). Restart IIS, and Note: You may have to reimport as "Complete certificate renewal" depending on your certificate.

    I'd also like to be able to set the Require Server Name Indication flag.

    You can refer to the link below:

    Setting "require server name indication" with c# using Microsoft.web.adminsitration.
    Programmatically add binding on IIS 8 with SNI option.
    IIS 8.0 Server Name Indication (SNI): SSL Scalability.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.