Created
April 8, 2014 03:10
-
-
Save damianh/10086352 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.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; | |
} | |
} | |
} |
Hi,
Thanks for the code snippet.
When I run this in Nancy.Owin
1.4.1 it give a compile error The type or namespace name 'NancyOwinHost' does not exist in the namespace 'Nancy.Owin' (are you missing an assembly reference?)
.
Do you know how do I get this to work with the latest build?
Thanks
Dave
Figured it out. I wanted:
var requestEnvironment = (IDictionary<string, object>) Context.Items["OWIN_REQUEST_ENVIRONMENT"];
var user = (IPrincipal) requestEnvironment["server.User"];
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Cruelio from here: https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-owin I think your missing package is:
Nancy.Owin