Skip to content

Instantly share code, notes, and snippets.

@jchannon
Forked from damianh/NancyOwinSelfHostWindowsAuth.cs
Last active September 22, 2015 11:40
Show Gist options
  • Save jchannon/7cbeddd37ef841f4c83d to your computer and use it in GitHub Desktop.
Save jchannon/7cbeddd37ef841f4c83d to your computer and use it in GitHub Desktop.
Nancy owin self hosting with Microsoft.Owin.HttpListener and windows authentication on .NET 4.0 (Personal opinion: avoid windows NTLM auth in web applications, even intranet ones. Use standards based SSO.)
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Principal;
using Microsoft.Owin.Hosting;
using Nancy;
using Owin;
internal class Program
{
private static void Main(string[] args)
{
using (WebApp.Start<Startup>("http://localhost:9000"))
{
Console.WriteLine("Press Enter to quit.");
Console.ReadKey();
}
}
}
internal class Startup
{
public void Configuration(IAppBuilder app)
{
var listener = (HttpListener) app.Properties["System.Net.HttpListener"];
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
app.UseNancy();
}
}
public class MyModule : NancyModule
{
public MyModule()
{
Get[""] = _ =>
{
var env = ((IDictionary<string, object>) Context.Items[Nancy.Owin.NancyOwinHost.RequestEnvironmentKey]);
var user = (IPrincipal) env["server.User"];
return "Hello " + user.Identity.Name;
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment