Owin self host console application with https support (no web api, no SignalR)











up vote
10
down vote

favorite
2












With SslStream and socket, I've developed a https web server from scratch.
I can apply a certificate to the stream from C# code and deal with the requests.



However, I didn't figure out how to do this with Owin.
Does any one know how to bind a certificate to a self hosted console application?



Example:



// Bind the below certificate to Owin host
var certificate = new X509Certificate2("server.pfx", "password");


Please refer to the existing Owin host code below for details:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin.Hosting;
using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;

namespace Owin.Startup
{
class Program
{
static void Main(string args)
{
int port = 8888;
string url = $"http://localhost:{port}";
using (WebApp.Start<Startup>(url))
{
Console.WriteLine($"Hosted: {url}");
Console.ReadLine();
}
}
}

public class Startup
{
private IAppBuilder app;
public void Configuration(IAppBuilder app)
{
#if DEBUG
app.UseErrorPage();
#endif

app.Use(new Func<AppFunc, AppFunc>(next => (async env =>
{
Console.WriteLine("Begin Request");
foreach (var i in env.Keys)
{
Console.WriteLine($"{i}t={(env[i] == null ? "null" : env[i].ToString())}t#t{(env[i] == null ? "null" : env[i].GetType().FullName)}");
}
if (next != null)
{
await next.Invoke(env);
}
else
{
Console.WriteLine("Process Complete");
}
Console.WriteLine("End Request");
})));

app.UseWelcomePage("/");

this.app = app;
}


}

}









share|improve this question




























    up vote
    10
    down vote

    favorite
    2












    With SslStream and socket, I've developed a https web server from scratch.
    I can apply a certificate to the stream from C# code and deal with the requests.



    However, I didn't figure out how to do this with Owin.
    Does any one know how to bind a certificate to a self hosted console application?



    Example:



    // Bind the below certificate to Owin host
    var certificate = new X509Certificate2("server.pfx", "password");


    Please refer to the existing Owin host code below for details:



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Owin.Hosting;
    using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;

    namespace Owin.Startup
    {
    class Program
    {
    static void Main(string args)
    {
    int port = 8888;
    string url = $"http://localhost:{port}";
    using (WebApp.Start<Startup>(url))
    {
    Console.WriteLine($"Hosted: {url}");
    Console.ReadLine();
    }
    }
    }

    public class Startup
    {
    private IAppBuilder app;
    public void Configuration(IAppBuilder app)
    {
    #if DEBUG
    app.UseErrorPage();
    #endif

    app.Use(new Func<AppFunc, AppFunc>(next => (async env =>
    {
    Console.WriteLine("Begin Request");
    foreach (var i in env.Keys)
    {
    Console.WriteLine($"{i}t={(env[i] == null ? "null" : env[i].ToString())}t#t{(env[i] == null ? "null" : env[i].GetType().FullName)}");
    }
    if (next != null)
    {
    await next.Invoke(env);
    }
    else
    {
    Console.WriteLine("Process Complete");
    }
    Console.WriteLine("End Request");
    })));

    app.UseWelcomePage("/");

    this.app = app;
    }


    }

    }









    share|improve this question


























      up vote
      10
      down vote

      favorite
      2









      up vote
      10
      down vote

      favorite
      2






      2





      With SslStream and socket, I've developed a https web server from scratch.
      I can apply a certificate to the stream from C# code and deal with the requests.



      However, I didn't figure out how to do this with Owin.
      Does any one know how to bind a certificate to a self hosted console application?



      Example:



      // Bind the below certificate to Owin host
      var certificate = new X509Certificate2("server.pfx", "password");


      Please refer to the existing Owin host code below for details:



      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using Microsoft.Owin.Hosting;
      using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;

      namespace Owin.Startup
      {
      class Program
      {
      static void Main(string args)
      {
      int port = 8888;
      string url = $"http://localhost:{port}";
      using (WebApp.Start<Startup>(url))
      {
      Console.WriteLine($"Hosted: {url}");
      Console.ReadLine();
      }
      }
      }

      public class Startup
      {
      private IAppBuilder app;
      public void Configuration(IAppBuilder app)
      {
      #if DEBUG
      app.UseErrorPage();
      #endif

      app.Use(new Func<AppFunc, AppFunc>(next => (async env =>
      {
      Console.WriteLine("Begin Request");
      foreach (var i in env.Keys)
      {
      Console.WriteLine($"{i}t={(env[i] == null ? "null" : env[i].ToString())}t#t{(env[i] == null ? "null" : env[i].GetType().FullName)}");
      }
      if (next != null)
      {
      await next.Invoke(env);
      }
      else
      {
      Console.WriteLine("Process Complete");
      }
      Console.WriteLine("End Request");
      })));

      app.UseWelcomePage("/");

      this.app = app;
      }


      }

      }









      share|improve this question















      With SslStream and socket, I've developed a https web server from scratch.
      I can apply a certificate to the stream from C# code and deal with the requests.



      However, I didn't figure out how to do this with Owin.
      Does any one know how to bind a certificate to a self hosted console application?



      Example:



      // Bind the below certificate to Owin host
      var certificate = new X509Certificate2("server.pfx", "password");


      Please refer to the existing Owin host code below for details:



      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using Microsoft.Owin.Hosting;
      using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;

      namespace Owin.Startup
      {
      class Program
      {
      static void Main(string args)
      {
      int port = 8888;
      string url = $"http://localhost:{port}";
      using (WebApp.Start<Startup>(url))
      {
      Console.WriteLine($"Hosted: {url}");
      Console.ReadLine();
      }
      }
      }

      public class Startup
      {
      private IAppBuilder app;
      public void Configuration(IAppBuilder app)
      {
      #if DEBUG
      app.UseErrorPage();
      #endif

      app.Use(new Func<AppFunc, AppFunc>(next => (async env =>
      {
      Console.WriteLine("Begin Request");
      foreach (var i in env.Keys)
      {
      Console.WriteLine($"{i}t={(env[i] == null ? "null" : env[i].ToString())}t#t{(env[i] == null ? "null" : env[i].GetType().FullName)}");
      }
      if (next != null)
      {
      await next.Invoke(env);
      }
      else
      {
      Console.WriteLine("Process Complete");
      }
      Console.WriteLine("End Request");
      })));

      app.UseWelcomePage("/");

      this.app = app;
      }


      }

      }






      c# ssl asp.net-web-api owin self-hosting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 11 at 1:10









      abatishchev

      68.5k69259389




      68.5k69259389










      asked Nov 19 '15 at 7:36









      mind1n

      4861822




      4861822
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          It depends on the server. Here's how to do it with HttpListener: http://katanaproject.codeplex.com/wikipage?title=Selfhosting&referringTitle=Documentation






          share|improve this answer





















          • I tried this but got the connection reset issue. Can you help to check the code at the following url? Thanks. stackoverflow.com/questions/33820731/…
            – mind1n
            Nov 20 '15 at 7:43




















          up vote
          0
          down vote













          Owin self hosting applications just need to bind to the proper URLs in code, while the certificate mappings should be done separately via Windows HTTP API.



          netsh http show sslcert can show you the existing mappings, and Jexus Manager provides the UI.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33797748%2fowin-self-host-console-application-with-https-support-no-web-api-no-signalr%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            It depends on the server. Here's how to do it with HttpListener: http://katanaproject.codeplex.com/wikipage?title=Selfhosting&referringTitle=Documentation






            share|improve this answer





















            • I tried this but got the connection reset issue. Can you help to check the code at the following url? Thanks. stackoverflow.com/questions/33820731/…
              – mind1n
              Nov 20 '15 at 7:43

















            up vote
            0
            down vote













            It depends on the server. Here's how to do it with HttpListener: http://katanaproject.codeplex.com/wikipage?title=Selfhosting&referringTitle=Documentation






            share|improve this answer





















            • I tried this but got the connection reset issue. Can you help to check the code at the following url? Thanks. stackoverflow.com/questions/33820731/…
              – mind1n
              Nov 20 '15 at 7:43















            up vote
            0
            down vote










            up vote
            0
            down vote









            It depends on the server. Here's how to do it with HttpListener: http://katanaproject.codeplex.com/wikipage?title=Selfhosting&referringTitle=Documentation






            share|improve this answer












            It depends on the server. Here's how to do it with HttpListener: http://katanaproject.codeplex.com/wikipage?title=Selfhosting&referringTitle=Documentation







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 '15 at 23:13









            Tratcher

            4,8041729




            4,8041729












            • I tried this but got the connection reset issue. Can you help to check the code at the following url? Thanks. stackoverflow.com/questions/33820731/…
              – mind1n
              Nov 20 '15 at 7:43




















            • I tried this but got the connection reset issue. Can you help to check the code at the following url? Thanks. stackoverflow.com/questions/33820731/…
              – mind1n
              Nov 20 '15 at 7:43


















            I tried this but got the connection reset issue. Can you help to check the code at the following url? Thanks. stackoverflow.com/questions/33820731/…
            – mind1n
            Nov 20 '15 at 7:43






            I tried this but got the connection reset issue. Can you help to check the code at the following url? Thanks. stackoverflow.com/questions/33820731/…
            – mind1n
            Nov 20 '15 at 7:43














            up vote
            0
            down vote













            Owin self hosting applications just need to bind to the proper URLs in code, while the certificate mappings should be done separately via Windows HTTP API.



            netsh http show sslcert can show you the existing mappings, and Jexus Manager provides the UI.






            share|improve this answer

























              up vote
              0
              down vote













              Owin self hosting applications just need to bind to the proper URLs in code, while the certificate mappings should be done separately via Windows HTTP API.



              netsh http show sslcert can show you the existing mappings, and Jexus Manager provides the UI.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Owin self hosting applications just need to bind to the proper URLs in code, while the certificate mappings should be done separately via Windows HTTP API.



                netsh http show sslcert can show you the existing mappings, and Jexus Manager provides the UI.






                share|improve this answer












                Owin self hosting applications just need to bind to the proper URLs in code, while the certificate mappings should be done separately via Windows HTTP API.



                netsh http show sslcert can show you the existing mappings, and Jexus Manager provides the UI.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 21 at 4:21









                Lex Li

                41.1k66894




                41.1k66894






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33797748%2fowin-self-host-console-application-with-https-support-no-web-api-no-signalr%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python