Skip to content

Instantly share code, notes, and snippets.

@haukurk
Last active November 8, 2018 17:58
Show Gist options
  • Save haukurk/52c762bc763784b69ad8999157c9e3eb to your computer and use it in GitHub Desktop.
Save haukurk/52c762bc763784b69ad8999157c9e3eb to your computer and use it in GitHub Desktop.
.NET Core NancyFx with IdentityServer4
FROM microsoft/dotnet:1.1.1-sdk
COPY ./NancyAPI.csproj /app/
WORKDIR /app/
RUN dotnet restore
ADD ./ /app/
RUN dotnet publish -c Debug
EXPOSE 5000
ENTRYPOINT ["dotnet", "run"]
using Nancy;
using Nancy.Security;
using System;
using System.Linq;
using System.Security.Claims;
namespace NancyAPI.Module
{
public class HomeModule : NancyModule
{
public HomeModule() : base("/")
{
this.RequiresAuthentication();
Get("/", args => "Hello World. This is pretty cool!");
Get("/test/{name}", args => new Person() { Name = args.name });
Get("/claims", args =>
{
this.RequiresClaims(claim => claim.Type == "role" && claim.Value == "test1");
return "Congratz, You have role test1";
});
}
public class Person
{
public string Name { get; set; }
}
}
}
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
namespace NancyAPI
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel()
.UseStartup<Startup>()
.UseUrls("http://0.0.0.0:5009")
.Build();
host.Run();
}
}
}
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nancy.Bootstrapper;
using Nancy.Owin;
using Nancy.TinyIoc;
namespace NancyAPI
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddWebEncoders();
}
public void Configure(IApplicationBuilder app)
{
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = "https://identity.hauxi.is",
RequireHttpsMetadata = true,
ApiName = "nancyapi"
});
app.UseOwin(x => x.UseNancy());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment